优化压缩相关的实现

hdrplus
Matthew 12 months ago
parent 0544b29f2a
commit 036d74c620

@ -10,7 +10,7 @@ import android.text.TextUtils;
import android.util.Base64; import android.util.Base64;
import android.util.Log; import android.util.Log;
import com.xypower.common.FileUtils; import com.xypower.common.FilesUtils;
import com.xypower.common.JSONUtils; import com.xypower.common.JSONUtils;
import com.xypower.common.MicroPhotoContext; import com.xypower.common.MicroPhotoContext;
import com.xypower.mpapp.v2.Camera2VideoActivity; import com.xypower.mpapp.v2.Camera2VideoActivity;
@ -73,8 +73,8 @@ public class BridgeActivity extends AppCompatActivity {
boolean res = MicroPhotoService.genKeys(index); boolean res = MicroPhotoService.genKeys(index);
String path = intent.getStringExtra("path"); String path = intent.getStringExtra("path");
if (!TextUtils.isEmpty(path)) { if (!TextUtils.isEmpty(path)) {
FileUtils.ensureParentDirectoryExisted(path); FilesUtils.ensureParentDirectoryExisted(path);
FileUtils.writeTextFile(path, res ? "1" : "0"); FilesUtils.writeTextFile(path, res ? "1" : "0");
} }
} else if (TextUtils.equals(action, ACTION_CERT_REQ)) { } else if (TextUtils.equals(action, ACTION_CERT_REQ)) {
int index = intent.getIntExtra("index", 0); int index = intent.getIntExtra("index", 0);
@ -90,8 +90,8 @@ public class BridgeActivity extends AppCompatActivity {
int bv = MicroPhotoService.getGpioInt(117); int bv = MicroPhotoService.getGpioInt(117);
int bcv = MicroPhotoService.getGpioInt(112); int bcv = MicroPhotoService.getGpioInt(112);
if (!TextUtils.isEmpty(path)) { if (!TextUtils.isEmpty(path)) {
FileUtils.ensureParentDirectoryExisted(path); FilesUtils.ensureParentDirectoryExisted(path);
FileUtils.writeTextFile(path + ".tmp", Integer.toString(bv) + " " + Integer.toString(bcv)); FilesUtils.writeTextFile(path + ".tmp", Integer.toString(bv) + " " + Integer.toString(bcv));
File file = new File(path + ".tmp"); File file = new File(path + ".tmp");
file.renameTo(new File(path)); file.renameTo(new File(path));
} }
@ -118,7 +118,7 @@ public class BridgeActivity extends AppCompatActivity {
if (configFile.exists()) { if (configFile.exists()) {
try { try {
FileUtils.copyFile(configFile, tmpConfigFile); FilesUtils.copyFile(configFile, tmpConfigFile);
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
@ -155,7 +155,7 @@ public class BridgeActivity extends AppCompatActivity {
if (file.exists()) { if (file.exists()) {
file.delete(); file.delete();
} else { } else {
FileUtils.ensureParentDirectoryExisted(path); FilesUtils.ensureParentDirectoryExisted(path);
} }
MicroPhotoService.takePhoto(channel, preset, true, tmpConfigFile.getAbsolutePath(), path); MicroPhotoService.takePhoto(channel, preset, true, tmpConfigFile.getAbsolutePath(), path);

@ -3,7 +3,6 @@ package com.xypower.mpapp;
import android.app.Service; import android.app.Service;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.IBinder; import android.os.IBinder;
import android.text.TextUtils; import android.text.TextUtils;
@ -12,7 +11,7 @@ import android.util.Log;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.xypower.common.FileUtils; import com.xypower.common.FilesUtils;
import com.xypower.common.JSONUtils; import com.xypower.common.JSONUtils;
import com.xypower.common.MicroPhotoContext; import com.xypower.common.MicroPhotoContext;
import com.xypower.mpapp.v2.Camera2VideoActivity; import com.xypower.mpapp.v2.Camera2VideoActivity;
@ -84,8 +83,8 @@ public class BridgeService extends Service {
boolean res = MicroPhotoService.genKeys(index); boolean res = MicroPhotoService.genKeys(index);
String path = intent.getStringExtra("path"); String path = intent.getStringExtra("path");
if (!TextUtils.isEmpty(path)) { if (!TextUtils.isEmpty(path)) {
FileUtils.ensureParentDirectoryExisted(path); FilesUtils.ensureParentDirectoryExisted(path);
FileUtils.writeTextFile(path, res ? "1" : "0"); FilesUtils.writeTextFile(path, res ? "1" : "0");
} }
} else if (TextUtils.equals(action, ACTION_CERT_REQ)) { } else if (TextUtils.equals(action, ACTION_CERT_REQ)) {
int index = intent.getIntExtra("index", 0); int index = intent.getIntExtra("index", 0);
@ -101,8 +100,8 @@ public class BridgeService extends Service {
int bv = MicroPhotoService.getGpioInt(117); int bv = MicroPhotoService.getGpioInt(117);
int bcv = MicroPhotoService.getGpioInt(112); int bcv = MicroPhotoService.getGpioInt(112);
if (!TextUtils.isEmpty(path)) { if (!TextUtils.isEmpty(path)) {
FileUtils.ensureParentDirectoryExisted(path); FilesUtils.ensureParentDirectoryExisted(path);
FileUtils.writeTextFile(path + ".tmp", Integer.toString(bv) + " " + Integer.toString(bcv)); FilesUtils.writeTextFile(path + ".tmp", Integer.toString(bv) + " " + Integer.toString(bcv));
File file = new File(path + ".tmp"); File file = new File(path + ".tmp");
file.renameTo(new File(path)); file.renameTo(new File(path));
} }
@ -129,7 +128,7 @@ public class BridgeService extends Service {
if (configFile.exists()) { if (configFile.exists()) {
try { try {
FileUtils.copyFile(configFile, tmpConfigFile); FilesUtils.copyFile(configFile, tmpConfigFile);
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
@ -166,7 +165,7 @@ public class BridgeService extends Service {
if (file.exists()) { if (file.exists()) {
file.delete(); file.delete();
} else { } else {
FileUtils.ensureParentDirectoryExisted(path); FilesUtils.ensureParentDirectoryExisted(path);
} }
MicroPhotoService.takePhoto(channel, preset, true, tmpConfigFile.getAbsolutePath(), path); MicroPhotoService.takePhoto(channel, preset, true, tmpConfigFile.getAbsolutePath(), path);

@ -5,6 +5,7 @@ import android.text.TextUtils;
import org.w3c.dom.Text; import org.w3c.dom.Text;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
@ -17,9 +18,7 @@ import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
public class FileUtils { public class FilesUtils {
public static void copyFile(File source, File dest) throws IOException { public static void copyFile(File source, File dest) throws IOException {
InputStream input = null; InputStream input = null;
@ -44,6 +43,15 @@ public class FileUtils {
} }
} }
public static void closeFriendly(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (Exception ex) {
}
closeable = null;
}
}
public static boolean DeleteFilesInPath(String path, long seconds) { public static boolean DeleteFilesInPath(String path, long seconds) {
File pathFile = new File(path); File pathFile = new File(path);
if (!pathFile.exists() || !pathFile.isDirectory()) { if (!pathFile.exists() || !pathFile.isDirectory()) {

@ -169,17 +169,17 @@ public class MicroPhotoContext {
File path = new File(appPath + "data/imgparams/"); File path = new File(appPath + "data/imgparams/");
if (path.exists() && path.isDirectory()) { if (path.exists() && path.isDirectory()) {
FileUtils.DeleteFilesInPath(path.getAbsolutePath()); FilesUtils.DeleteFilesInPath(path.getAbsolutePath());
} }
path = new File(appPath + "data/schedules/"); path = new File(appPath + "data/schedules/");
if (path.exists() && path.isDirectory()) { if (path.exists() && path.isDirectory()) {
FileUtils.DeleteFilesInPath(path.getAbsolutePath()); FilesUtils.DeleteFilesInPath(path.getAbsolutePath());
} }
path = new File(appPath + "data/videoparams/"); path = new File(appPath + "data/videoparams/");
if (path.exists() && path.isDirectory()) { if (path.exists() && path.isDirectory()) {
FileUtils.DeleteFilesInPath(path.getAbsolutePath()); FilesUtils.DeleteFilesInPath(path.getAbsolutePath());
} }
path = new File(appPath + "data/sampling"); path = new File(appPath + "data/sampling");
@ -232,7 +232,7 @@ public class MicroPhotoContext {
AppConfig appConfig = new AppConfig(); AppConfig appConfig = new AppConfig();
try { try {
String content = FileUtils.readTextFile(path); String content = FilesUtils.readTextFile(path);
JSONObject jsonObject = TextUtils.isEmpty(content) ? new JSONObject() : new JSONObject(content); JSONObject jsonObject = TextUtils.isEmpty(content) ? new JSONObject() : new JSONObject(content);
appConfig.cmdid = jsonObject.optString(jsonObject.has("cmdid") ? "cmdid" : "CMDID", ""); appConfig.cmdid = jsonObject.optString(jsonObject.has("cmdid") ? "cmdid" : "CMDID", "");
@ -262,7 +262,7 @@ public class MicroPhotoContext {
String appPath = buildMasterAppDir(context); String appPath = buildMasterAppDir(context);
try { try {
String content = FileUtils.readTextFile(appPath + "data/Master.json"); String content = FilesUtils.readTextFile(appPath + "data/Master.json");
JSONObject jsonObject = TextUtils.isEmpty(content) ? new JSONObject() : new JSONObject(content); JSONObject jsonObject = TextUtils.isEmpty(content) ? new JSONObject() : new JSONObject(content);
masterConfig.server = jsonObject.optString("server", ""); masterConfig.server = jsonObject.optString("server", "");
@ -314,7 +314,7 @@ public class MicroPhotoContext {
String path = appPath + "data/Master.json"; String path = appPath + "data/Master.json";
String content = FileUtils.readTextFile(path); String content = FilesUtils.readTextFile(path);
JSONObject jsonObject = TextUtils.isEmpty(content) ? new JSONObject() : new JSONObject(content); JSONObject jsonObject = TextUtils.isEmpty(content) ? new JSONObject() : new JSONObject(content);
jsonObject.put("server", masterConfig.server); jsonObject.put("server", masterConfig.server);
@ -427,7 +427,7 @@ public class MicroPhotoContext {
dataPath.mkdirs(); dataPath.mkdirs();
} }
String content = FileUtils.readTextFile(path + "data/App.json"); String content = FilesUtils.readTextFile(path + "data/App.json");
JSONObject jsonObject = TextUtils.isEmpty(content) ? new JSONObject() : new JSONObject(content); JSONObject jsonObject = TextUtils.isEmpty(content) ? new JSONObject() : new JSONObject(content);
jsonObject.put("CMDID", appConfig.cmdid); jsonObject.put("CMDID", appConfig.cmdid);

@ -4,85 +4,106 @@ import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream; import java.util.zip.ZipOutputStream;
public class ZipUtils { public class ZipUtils {
public static interface Filter {
boolean match(String file);
}
public static void ZipFolder(File srcFile, File zipFile, Filter filter) {
try {
ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(zipFile));
ZipFiles(srcFile.getParent() + File.separator, srcFile.getName(), outZip, filter);
outZip.finish();
outZip.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void ZipFolders(List<File> srcFiles, File zipFile, Filter filter) { public static void ZipFolder(File srcDirectory, File zipFile, FilenameFilter filter) {
ZipOutputStream outZip = null;
FileInputStream inputStream = null;
try { try {
ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(zipFile)); outZip = new ZipOutputStream(new FileOutputStream(zipFile));
for (File srcFile : srcFiles) { int len;
ZipFiles(srcFile.getParent() + File.separator, srcFile.getName(), outZip, filter); byte[] buffer = new byte[1024 * 256];
ZipEntry zipEntry = null;
File[] subFiles = srcDirectory.listFiles(filter);
if (subFiles != null && subFiles.length > 0) {
for (File subFile : subFiles) {
zipEntry = new ZipEntry(subFile.getName());
outZip.putNextEntry(zipEntry);
inputStream = new FileInputStream(subFile);
while ((len = inputStream.read(buffer)) != -1) {
outZip.write(buffer, 0, len);
}
FilesUtils.closeFriendly(inputStream);
outZip.closeEntry();
}
} }
outZip.finish();
outZip.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally {
FilesUtils.closeFriendly(inputStream);
try {
outZip.finish();
} catch (Exception ex) {
ex.printStackTrace();
}
FilesUtils.closeFriendly(outZip);
} }
} }
private static void ZipFiles(String srcFileParentName, String srcFileName, ZipOutputStream zipOutputSteam, Filter filter) throws Exception { public static void ZipFolders(Map<String, File> srcDirectories, File zipFile, FilenameFilter filter) {
if (zipOutputSteam == null) ZipOutputStream outZip = null;
return; FileInputStream inputStream = null;
File file = new File(srcFileParentName + srcFileName);
if (file.isFile()) { try {
ZipEntry zipEntry = new ZipEntry(srcFileName); outZip = new ZipOutputStream(new FileOutputStream(zipFile));
FileInputStream inputStream = new FileInputStream(file);
zipOutputSteam.putNextEntry(zipEntry);
int len; int len;
byte[] buffer = new byte[1024 * 256]; byte[] buffer = new byte[1024 * 256];
while ((len = inputStream.read(buffer)) != -1) { ZipEntry zipEntry = null;
zipOutputSteam.write(buffer, 0, len);
} for (Map.Entry<String, File> srcDirectory : srcDirectories.entrySet()) {
zipOutputSteam.closeEntry(); File[] subFiles = srcDirectory.getValue().listFiles(filter);
} else { if (subFiles == null || subFiles.length == 0) {
// folder
String fileList[] = file.list();
// NO sub file
if (fileList.length <= 0) {
ZipEntry zipEntry = new ZipEntry(srcFileName + File.separator);
zipOutputSteam.putNextEntry(zipEntry);
zipOutputSteam.closeEntry();
}
//
for (int i = 0; i < fileList.length; i++) {
if (filter != null && !filter.match(fileList[i])) {
continue; continue;
} }
ZipFiles(srcFileParentName + srcFileName + "/", fileList[i], zipOutputSteam, filter); for (File subFile : subFiles) {
zipEntry = new ZipEntry(srcDirectory.getKey() + File.separator + subFile.getName());
outZip.putNextEntry(zipEntry);
inputStream = new FileInputStream(subFile);
while ((len = inputStream.read(buffer)) != -1) {
outZip.write(buffer, 0, len);
}
FilesUtils.closeFriendly(inputStream);
outZip.closeEntry();
}
} }
} catch (Exception e) {
e.printStackTrace();
} finally {
FilesUtils.closeFriendly(inputStream);
try {
outZip.finish();
} catch (Exception ex) {
ex.printStackTrace();
}
FilesUtils.closeFriendly(outZip);
} }
} }
public static void ZipFiles(List<String> srcFiles, File zipFile) { public static void ZipFiles(List<String> srcFiles, File zipFile) {
ZipOutputStream outZip = null;
FileInputStream inputStream = null;
try { try {
ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(zipFile)); outZip = new ZipOutputStream(new FileOutputStream(zipFile));
// ZipFiles(srcFile.getParent() + File.separator, srcFile.getName(), outZip); // ZipFiles(srcFile.getParent() + File.separator, srcFile.getName(), outZip);
int len = 0;
byte[] buffer = new byte[1024 * 256]; byte[] buffer = new byte[1024 * 256];
for (String path : srcFiles) { for (String path : srcFiles) {
@ -91,24 +112,29 @@ public class ZipUtils {
continue; continue;
} }
ZipEntry zipEntry = new ZipEntry(path); ZipEntry zipEntry = new ZipEntry(path);
FileInputStream inputStream = new FileInputStream(file); inputStream = new FileInputStream(file);
outZip.putNextEntry(zipEntry); outZip.putNextEntry(zipEntry);
int len;
while ((len = inputStream.read(buffer)) != -1) { while ((len = inputStream.read(buffer)) != -1) {
outZip.write(buffer, 0, len); outZip.write(buffer, 0, len);
} }
FilesUtils.closeFriendly(inputStream);
outZip.closeEntry(); outZip.closeEntry();
} }
outZip.finish();
outZip.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally {
FilesUtils.closeFriendly(inputStream);
if (outZip != null) {
try {
outZip.finish();
} catch (Exception ex) {
ex.printStackTrace();
}
}
FilesUtils.closeFriendly(outZip);
} }
} }
} }

@ -33,6 +33,7 @@ import java.io.BufferedReader;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
@ -48,7 +49,9 @@ import java.nio.file.StandardCopyOption;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
public class AppMaster { public class AppMaster {
@ -922,16 +925,16 @@ public class AppMaster {
final String mpAppDir = MicroPhotoContext.buildMpAppDir(mService.getApplicationContext()); final String mpAppDir = MicroPhotoContext.buildMpAppDir(mService.getApplicationContext());
final File logDir = new File(mpAppDir + "logs" + File.separator); final File logDir = new File(mpAppDir + "logs" + File.separator);
List<File> folders = new ArrayList<>(); Map<String, File> folders = new HashMap<>();
if (logDir.exists()) { if (logDir.exists()) {
folders.add(logDir); folders.put("logs", logDir);
} }
final String masterAppDir = MicroPhotoContext.buildMasterAppDir(mService.getApplicationContext()); final String masterAppDir = MicroPhotoContext.buildMasterAppDir(mService.getApplicationContext());
final File mlogDir = new File(masterAppDir + "logs" + File.separator); final File mlogDir = new File(masterAppDir + "logs" + File.separator);
if (logDir.exists()) { if (logDir.exists()) {
folders.add(mlogDir); folders.put("mlogs", mlogDir);
} }
if (folders.isEmpty()) { if (folders.isEmpty()) {
@ -939,15 +942,16 @@ public class AppMaster {
return; return;
} }
ZipUtils.Filter filter = new ZipUtils.Filter() { FilenameFilter filter = new FilenameFilter() {
@Override @Override
public boolean match(String fileName) { public boolean accept(File dir, String name) {
if (!fileName.endsWith(".txt")) { if (!name.endsWith(".txt")) {
return false; return false;
} }
if (!includingSpecData && fileName.startsWith("specdata")) { if (!includingSpecData && name.startsWith("specdata")) {
return false; return false;
} }
return true; return true;
} }
}; };
@ -991,9 +995,9 @@ public class AppMaster {
return; return;
} }
ZipUtils.Filter filter = new ZipUtils.Filter() { FilenameFilter filter = new FilenameFilter() {
@Override @Override
public boolean match(String fileName) { public boolean accept(File dir, String name) {
return fileName.endsWith(".txt"); return fileName.endsWith(".txt");
} }
}; };

@ -135,8 +135,6 @@ public class MainActivity extends AppCompatActivity {
stringBuilder.append("\r\n"); stringBuilder.append("\r\n");
((TextView) findViewById((R.id.cmdid))).setText(stringBuilder.toString()); ((TextView) findViewById((R.id.cmdid))).setText(stringBuilder.toString());
} }
@Override @Override

@ -6,9 +6,7 @@ import android.content.Intent;
import android.os.Build; import android.os.Build;
import android.os.Environment; import android.os.Environment;
import androidx.annotation.RequiresApi; import com.xypower.common.FilesUtils;
import com.xypower.common.FileUtils;
import com.xypower.common.JSONUtils; import com.xypower.common.JSONUtils;
import com.xypower.common.MicroPhotoContext; import com.xypower.common.MicroPhotoContext;
@ -303,12 +301,12 @@ public class UpdateSysConfigUtil {
File path = new File(appPath + "photos/"); File path = new File(appPath + "photos/");
if (path.exists() && path.isDirectory()) { if (path.exists() && path.isDirectory()) {
FileUtils.DeleteFilesInPath(path.getAbsolutePath()); FilesUtils.DeleteFilesInPath(path.getAbsolutePath());
} }
path = new File(appPath + "sentPhotos/"); path = new File(appPath + "sentPhotos/");
if (path.exists() && path.isDirectory()) { if (path.exists() && path.isDirectory()) {
FileUtils.DeleteFilesInPath(path.getAbsolutePath()); FilesUtils.DeleteFilesInPath(path.getAbsolutePath());
} }
} }
@ -320,12 +318,12 @@ public class UpdateSysConfigUtil {
File path = new File(appPath + "logs/"); File path = new File(appPath + "logs/");
if (path.exists() && path.isDirectory()) { if (path.exists() && path.isDirectory()) {
FileUtils.DeleteFilesInPath(path.getAbsolutePath()); FilesUtils.DeleteFilesInPath(path.getAbsolutePath());
} }
path = new File(masterAppDir + "logs/"); path = new File(masterAppDir + "logs/");
if (path.exists() && path.isDirectory()) { if (path.exists() && path.isDirectory()) {
FileUtils.DeleteFilesInPath(path.getAbsolutePath()); FilesUtils.DeleteFilesInPath(path.getAbsolutePath());
} }
} }

Loading…
Cancel
Save