优化压缩相关的实现

hdrplus
Matthew 12 months ago
parent 0544b29f2a
commit 036d74c620

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

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

@ -5,6 +5,7 @@ import android.text.TextUtils;
import org.w3c.dom.Text;
import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
@ -17,9 +18,7 @@ import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class FileUtils {
public class FilesUtils {
public static void copyFile(File source, File dest) throws IOException {
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) {
File pathFile = new File(path);
if (!pathFile.exists() || !pathFile.isDirectory()) {

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

@ -4,85 +4,106 @@ import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
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 {
ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(zipFile));
for (File srcFile : srcFiles) {
ZipFiles(srcFile.getParent() + File.separator, srcFile.getName(), outZip, filter);
outZip = new ZipOutputStream(new FileOutputStream(zipFile));
int len;
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) {
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 {
if (zipOutputSteam == null)
return;
File file = new File(srcFileParentName + srcFileName);
public static void ZipFolders(Map<String, File> srcDirectories, File zipFile, FilenameFilter filter) {
ZipOutputStream outZip = null;
FileInputStream inputStream = null;
if (file.isFile()) {
ZipEntry zipEntry = new ZipEntry(srcFileName);
FileInputStream inputStream = new FileInputStream(file);
zipOutputSteam.putNextEntry(zipEntry);
try {
outZip = new ZipOutputStream(new FileOutputStream(zipFile));
int len;
byte[] buffer = new byte[1024 * 256];
while ((len = inputStream.read(buffer)) != -1) {
zipOutputSteam.write(buffer, 0, len);
}
zipOutputSteam.closeEntry();
} else {
// 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])) {
ZipEntry zipEntry = null;
for (Map.Entry<String, File> srcDirectory : srcDirectories.entrySet()) {
File[] subFiles = srcDirectory.getValue().listFiles(filter);
if (subFiles == null || subFiles.length == 0) {
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) {
ZipOutputStream outZip = null;
FileInputStream inputStream = null;
try {
ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(zipFile));
outZip = new ZipOutputStream(new FileOutputStream(zipFile));
// ZipFiles(srcFile.getParent() + File.separator, srcFile.getName(), outZip);
int len = 0;
byte[] buffer = new byte[1024 * 256];
for (String path : srcFiles) {
@ -91,24 +112,29 @@ public class ZipUtils {
continue;
}
ZipEntry zipEntry = new ZipEntry(path);
FileInputStream inputStream = new FileInputStream(file);
inputStream = new FileInputStream(file);
outZip.putNextEntry(zipEntry);
int len;
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) {
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.File;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@ -48,7 +49,9 @@ import java.nio.file.StandardCopyOption;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class AppMaster {
@ -922,16 +925,16 @@ public class AppMaster {
final String mpAppDir = MicroPhotoContext.buildMpAppDir(mService.getApplicationContext());
final File logDir = new File(mpAppDir + "logs" + File.separator);
List<File> folders = new ArrayList<>();
Map<String, File> folders = new HashMap<>();
if (logDir.exists()) {
folders.add(logDir);
folders.put("logs", logDir);
}
final String masterAppDir = MicroPhotoContext.buildMasterAppDir(mService.getApplicationContext());
final File mlogDir = new File(masterAppDir + "logs" + File.separator);
if (logDir.exists()) {
folders.add(mlogDir);
folders.put("mlogs", mlogDir);
}
if (folders.isEmpty()) {
@ -939,15 +942,16 @@ public class AppMaster {
return;
}
ZipUtils.Filter filter = new ZipUtils.Filter() {
FilenameFilter filter = new FilenameFilter() {
@Override
public boolean match(String fileName) {
if (!fileName.endsWith(".txt")) {
public boolean accept(File dir, String name) {
if (!name.endsWith(".txt")) {
return false;
}
if (!includingSpecData && fileName.startsWith("specdata")) {
if (!includingSpecData && name.startsWith("specdata")) {
return false;
}
return true;
}
};
@ -991,9 +995,9 @@ public class AppMaster {
return;
}
ZipUtils.Filter filter = new ZipUtils.Filter() {
FilenameFilter filter = new FilenameFilter() {
@Override
public boolean match(String fileName) {
public boolean accept(File dir, String name) {
return fileName.endsWith(".txt");
}
};

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

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

Loading…
Cancel
Save