package com.sitinspring.zip.test;
import java.io.File; import java.io.IOException; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipOutputStream; import de.schlichtherle.io.FileInputStream; import de.schlichtherle.io.FileOutputStream; public class MoreZip { public static void main(String[] args) { File[] f={new File("D:/中文/我们.txt"),new File("D:/中文/他们.txt"),new File("D:/中文/你们.txt")}; MoreZip.ZipFiles(f, new File("D:/你我他.zip")); } /** * 压缩文件 * @param srcfile File[] 需要压缩的文件列表 * @param zipfile File 压缩后的文件 */ public static void ZipFiles(java.io.File[] srcfile, java.io.File zipfile) { byte[] buf = new byte[1024]; try { // Create the ZIP file ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile)); // Compress the files for (int i = 0; i < srcfile.length; i++) { FileInputStream in = new FileInputStream(srcfile[i]); // Add ZIP entry to output stream. out.putNextEntry(new ZipEntry(srcfile[i].getName())); // Transfer bytes from the file to the ZIP file int len; while ( (len = in.read(buf)) > 0) { out.write(buf, 0, len); } // Complete the entry out.closeEntry(); in.close(); } // Complete the ZIP file out.close(); System.out.println("压缩完成."); } catch (IOException e) { e.printStackTrace(); } } |
|