优化日志压缩和上传

ndkvideo
Matthew 4 months ago
parent 0bfbe32077
commit a716856a4f

@ -6,6 +6,9 @@ import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.FilenameFilter; import java.io.FilenameFilter;
import java.io.IOException; import java.io.IOException;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
@ -15,6 +18,7 @@ public class ZipUtils {
public static void ZipFolder(File srcDirectory, File zipFile, FilenameFilter filter) { public static void ZipFolder(File srcDirectory, File zipFile, FilenameFilter filter) {
ZipOutputStream outZip = null; ZipOutputStream outZip = null;
WritableByteChannel writableByteChannel = null;
FileInputStream inputStream = null; FileInputStream inputStream = null;
FileOutputStream fileOutputStream = null; FileOutputStream fileOutputStream = null;
@ -22,8 +26,8 @@ public class ZipUtils {
fileOutputStream = new FileOutputStream(zipFile); fileOutputStream = new FileOutputStream(zipFile);
outZip = new ZipOutputStream(fileOutputStream); outZip = new ZipOutputStream(fileOutputStream);
int len; writableByteChannel = Channels.newChannel(outZip);
byte[] buffer = new byte[1024 * 256];
ZipEntry zipEntry = null; ZipEntry zipEntry = null;
File[] subFiles = srcDirectory.listFiles(filter); File[] subFiles = srcDirectory.listFiles(filter);
@ -34,8 +38,11 @@ public class ZipUtils {
inputStream = new FileInputStream(subFile); inputStream = new FileInputStream(subFile);
while ((len = inputStream.read(buffer)) != -1) { FileChannel fileChannel = inputStream.getChannel();
outZip.write(buffer, 0, len); try {
fileChannel.transferTo(0, fileChannel.size(), writableByteChannel);
} finally {
FilesUtils.closeFriendly(fileChannel);
} }
FilesUtils.closeFriendly(inputStream); FilesUtils.closeFriendly(inputStream);
@ -52,20 +59,22 @@ public class ZipUtils {
ex.printStackTrace(); ex.printStackTrace();
} }
FilesUtils.closeFriendly(fileOutputStream); FilesUtils.closeFriendly(fileOutputStream);
FilesUtils.closeFriendly(writableByteChannel);
FilesUtils.closeFriendly(outZip); FilesUtils.closeFriendly(outZip);
} }
} }
public static void ZipFolders(Map<String, File> srcDirectories, File zipFile, FilenameFilter filter) { public static void ZipFolders(Map<String, File> srcDirectories, File zipFile, FilenameFilter filter) {
ZipOutputStream outZip = null; ZipOutputStream outZip = null;
WritableByteChannel writableByteChannel = null;
FileInputStream inputStream = null; FileInputStream inputStream = null;
FileOutputStream fileOutputStream = null; FileOutputStream fileOutputStream = null;
try { try {
fileOutputStream = new FileOutputStream(zipFile); fileOutputStream = new FileOutputStream(zipFile);
outZip = new ZipOutputStream(fileOutputStream); outZip = new ZipOutputStream(fileOutputStream);
int len; writableByteChannel = Channels.newChannel(outZip);
byte[] buffer = new byte[1024 * 256];
ZipEntry zipEntry = null; ZipEntry zipEntry = null;
for (Map.Entry<String, File> srcDirectory : srcDirectories.entrySet()) { for (Map.Entry<String, File> srcDirectory : srcDirectories.entrySet()) {
@ -79,8 +88,11 @@ public class ZipUtils {
inputStream = new FileInputStream(subFile); inputStream = new FileInputStream(subFile);
while ((len = inputStream.read(buffer)) != -1) { FileChannel fileChannel = inputStream.getChannel();
outZip.write(buffer, 0, len); try {
fileChannel.transferTo(0, fileChannel.size(), writableByteChannel);
} finally {
FilesUtils.closeFriendly(fileChannel);
} }
FilesUtils.closeFriendly(inputStream); FilesUtils.closeFriendly(inputStream);
@ -97,7 +109,9 @@ public class ZipUtils {
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
FilesUtils.closeFriendly(fileOutputStream); FilesUtils.closeFriendly(fileOutputStream);
FilesUtils.closeFriendly(writableByteChannel);
FilesUtils.closeFriendly(outZip); FilesUtils.closeFriendly(outZip);
} }
} }
@ -105,13 +119,13 @@ public class ZipUtils {
public static void ZipFiles(List<String> srcFiles, File zipFile) { public static void ZipFiles(List<String> srcFiles, File zipFile) {
ZipOutputStream outZip = null; ZipOutputStream outZip = null;
WritableByteChannel writableByteChannel = null;
FileInputStream inputStream = null; FileInputStream inputStream = null;
FileOutputStream fileOutputStream = null; FileOutputStream fileOutputStream = null;
try { try {
fileOutputStream = new FileOutputStream(zipFile); fileOutputStream = new FileOutputStream(zipFile);
outZip = new ZipOutputStream(fileOutputStream); outZip = new ZipOutputStream(fileOutputStream);
int len = 0; writableByteChannel = Channels.newChannel(outZip);
byte[] buffer = new byte[1024 * 256];
for (String path : srcFiles) { for (String path : srcFiles) {
File file = new File(path); File file = new File(path);
@ -119,12 +133,16 @@ public class ZipUtils {
continue; continue;
} }
ZipEntry zipEntry = new ZipEntry(srcFiles.size() > 1 ? path.substring(1) : file.getName()); ZipEntry zipEntry = new ZipEntry(srcFiles.size() > 1 ? path.substring(1) : file.getName());
inputStream = new FileInputStream(file);
outZip.putNextEntry(zipEntry); outZip.putNextEntry(zipEntry);
while ((len = inputStream.read(buffer)) != -1) {
outZip.write(buffer, 0, len); inputStream = new FileInputStream(file);
FileChannel fileChannel = inputStream.getChannel();
try {
fileChannel.transferTo(0, fileChannel.size(), writableByteChannel);
} finally {
FilesUtils.closeFriendly(fileChannel);
} }
outZip.closeEntry(); outZip.closeEntry();
FilesUtils.closeFriendly(inputStream); FilesUtils.closeFriendly(inputStream);
} }
@ -141,6 +159,7 @@ public class ZipUtils {
} }
} }
FilesUtils.closeFriendly(fileOutputStream); FilesUtils.closeFriendly(fileOutputStream);
FilesUtils.closeFriendly(writableByteChannel);
FilesUtils.closeFriendly(outZip); FilesUtils.closeFriendly(outZip);
} }
} }

@ -1093,20 +1093,32 @@ public class AppMaster {
} }
}; };
ZipUtils.ZipFolders(folders, file, filter); try {
ZipUtils.ZipFolders(folders, file, filter);
} catch (Exception ex) {
ex.printStackTrace();
}
if (!file.exists()) { if (!file.exists()) {
return; return;
} }
FileUploader fileUploader = new FileUploader(url); mService.logger.info("Compressing Log Files finished File Size=" + Long.toString(file.length() / 1024) + "k");
fileUploader.addFilePart("file", file, fileName, "application/x-zip-compressed");
String response = fileUploader.finish(); try {
if (response != null) { FileUploader fileUploader = new FileUploader(url);
fileUploader.addFilePart("file", file, fileName, "application/x-zip-compressed");
String response = fileUploader.finish();
if (response != null) {
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
file.delete();
} }
file.delete();
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} }

Loading…
Cancel
Save