diff --git a/common/src/main/java/com/xypower/common/ZipUtils.java b/common/src/main/java/com/xypower/common/ZipUtils.java index c4038436..9835bbc8 100644 --- a/common/src/main/java/com/xypower/common/ZipUtils.java +++ b/common/src/main/java/com/xypower/common/ZipUtils.java @@ -6,6 +6,9 @@ import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FilenameFilter; 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.Map; import java.util.zip.ZipEntry; @@ -15,6 +18,7 @@ public class ZipUtils { public static void ZipFolder(File srcDirectory, File zipFile, FilenameFilter filter) { ZipOutputStream outZip = null; + WritableByteChannel writableByteChannel = null; FileInputStream inputStream = null; FileOutputStream fileOutputStream = null; @@ -22,8 +26,8 @@ public class ZipUtils { fileOutputStream = new FileOutputStream(zipFile); outZip = new ZipOutputStream(fileOutputStream); - int len; - byte[] buffer = new byte[1024 * 256]; + writableByteChannel = Channels.newChannel(outZip); + ZipEntry zipEntry = null; File[] subFiles = srcDirectory.listFiles(filter); @@ -34,8 +38,11 @@ public class ZipUtils { inputStream = new FileInputStream(subFile); - while ((len = inputStream.read(buffer)) != -1) { - outZip.write(buffer, 0, len); + FileChannel fileChannel = inputStream.getChannel(); + try { + fileChannel.transferTo(0, fileChannel.size(), writableByteChannel); + } finally { + FilesUtils.closeFriendly(fileChannel); } FilesUtils.closeFriendly(inputStream); @@ -52,20 +59,22 @@ public class ZipUtils { ex.printStackTrace(); } FilesUtils.closeFriendly(fileOutputStream); + FilesUtils.closeFriendly(writableByteChannel); FilesUtils.closeFriendly(outZip); } } public static void ZipFolders(Map srcDirectories, File zipFile, FilenameFilter filter) { ZipOutputStream outZip = null; + WritableByteChannel writableByteChannel = null; FileInputStream inputStream = null; FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream(zipFile); outZip = new ZipOutputStream(fileOutputStream); - int len; - byte[] buffer = new byte[1024 * 256]; + writableByteChannel = Channels.newChannel(outZip); + ZipEntry zipEntry = null; for (Map.Entry srcDirectory : srcDirectories.entrySet()) { @@ -79,8 +88,11 @@ public class ZipUtils { inputStream = new FileInputStream(subFile); - while ((len = inputStream.read(buffer)) != -1) { - outZip.write(buffer, 0, len); + FileChannel fileChannel = inputStream.getChannel(); + try { + fileChannel.transferTo(0, fileChannel.size(), writableByteChannel); + } finally { + FilesUtils.closeFriendly(fileChannel); } FilesUtils.closeFriendly(inputStream); @@ -97,7 +109,9 @@ public class ZipUtils { } catch (Exception ex) { ex.printStackTrace(); } + FilesUtils.closeFriendly(fileOutputStream); + FilesUtils.closeFriendly(writableByteChannel); FilesUtils.closeFriendly(outZip); } } @@ -105,13 +119,13 @@ public class ZipUtils { public static void ZipFiles(List srcFiles, File zipFile) { ZipOutputStream outZip = null; + WritableByteChannel writableByteChannel = null; FileInputStream inputStream = null; FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream(zipFile); outZip = new ZipOutputStream(fileOutputStream); - int len = 0; - byte[] buffer = new byte[1024 * 256]; + writableByteChannel = Channels.newChannel(outZip); for (String path : srcFiles) { File file = new File(path); @@ -119,12 +133,16 @@ public class ZipUtils { continue; } ZipEntry zipEntry = new ZipEntry(srcFiles.size() > 1 ? path.substring(1) : file.getName()); - inputStream = new FileInputStream(file); - 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(); FilesUtils.closeFriendly(inputStream); } @@ -141,6 +159,7 @@ public class ZipUtils { } } FilesUtils.closeFriendly(fileOutputStream); + FilesUtils.closeFriendly(writableByteChannel); FilesUtils.closeFriendly(outZip); } } diff --git a/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java b/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java index 84de9312..90b355c4 100644 --- a/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java +++ b/mpmaster/src/main/java/com/xypower/mpmaster/AppMaster.java @@ -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()) { return; } - FileUploader fileUploader = new FileUploader(url); - fileUploader.addFilePart("file", file, fileName, "application/x-zip-compressed"); + mService.logger.info("Compressing Log Files finished File Size=" + Long.toString(file.length() / 1024) + "k"); - String response = fileUploader.finish(); - if (response != null) { + try { + 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) { ex.printStackTrace(); }