public static void fileCompressMultiEn(java.io.File[] fileList, java.io.File compressedFile) { java.io.FileOutputStream fos = null; try { fos = new java.io.FileOutputStream(compressedFile); } catch (java.io.FileNotFoundException ex) { Ts.printErr(ex); } java.util.zip.ZipOutputStream zos = new java.util.zip.ZipOutputStream(fos); java.io.BufferedOutputStream bos = new java.io.BufferedOutputStream(zos); byte[] buff = new byte[1024 * 4]; try { for (int i = 0; i < fileList.length; i++) { java.io.BufferedInputStream bis = Ts.convertFileToBufferedInputStream(fileList[i]); zos.putNextEntry(new java.util.zip.ZipEntry(fileList[i].getName())); int len; while ((len = bis.read(buff)) >= 0) { bos.write(buff, 0, len); } // It is essential! bos.flush(); bis.close(); } bos.close(); } catch (java.io.IOException ex) { Ts.printErr(ex); } } public static void fileCompressMultiEn(String[] fileListName, String compressedFileName) { int len = fileListName.length; java.io.File[] fileList = new java.io.File[len]; for (int i = 0; i < len; i++) { fileList[i] = new java.io.File(fileListName[i]); } Ts.fileCompressMultiEn(fileList, new java.io.File(compressedFileName)); } public static void fileCompressMultiDe(java.io.File compressedFile, java.io.File targetDirectory) { java.io.FileInputStream fis = null; try { fis = new java.io.FileInputStream(compressedFile); } catch (java.io.FileNotFoundException ex) { Ts.printErr(ex); } java.util.zip.ZipInputStream zis = new java.util.zip.ZipInputStream(fis); java.io.BufferedInputStream bis = new java.io.BufferedInputStream(zis); java.util.zip.ZipEntry ze = null; byte[] buff = new byte[1024 * 4]; java.io.File createdDirectory = null; try { if (!(targetDirectory.exists() && targetDirectory.isDirectory())) { createdDirectory = Ts.fileCreateDirectory(targetDirectory.getCanonicalPath()); } else { createdDirectory = targetDirectory; } while ((ze = zis.getNextEntry()) != null) { java.io.BufferedOutputStream bos = new java.io.BufferedOutputStream(new java.io.FileOutputStream(createdDirectory.getCanonicalFile() + "/" + ze.getName())); int len = 0; while ((len = bis.read(buff)) >= 0) { bos.write(buff, 0, len); } bos.close(); } bis.close(); } catch (java.io.IOException ex) { Ts.printErr(ex); } } public static void fileCompressMultiDe(String compressedFileName, String targetDirectoryName) { Ts.fileCompressMultiDe(new java.io.File(compressedFileName), new java.io.File(targetDirectoryName)); } private static void fileCompressMultiTest() { String[] fileList = {"c:/1.jpg", "c:/result001.txt", "c:/result002.txt", "c:/result003.txt", "c:/result004.txt", "c:/result005.txt", "c:/chart.jpg"}; Ts.fileCompressMultiEn(fileList, "c:/test1.zip"); Ts.fileCompressMultiDe("c:/test1.zip", "c:/love"); } public static void printErr(java.lang.Exception ex) { ex.printStackTrace(); System.exit(-1); } public static java.io.BufferedInputStream convertFileToBufferedInputStream(java.io.File file) { java.io.BufferedInputStream bis = null; try { bis = new java.io.BufferedInputStream(new java.io.FileInputStream(file)); } catch (java.io.FileNotFoundException ex) { Ts.printErr(ex); } return bis; } public static java.io.File fileCreateDirectory(String dir) { java.io.File file = new java.io.File(dir); if (!file.mkdir()) { Ts.printErr("Fail to create directory: " + dir); } return file; }