Recently encountered multiple files in the project to reach the ZIP package, due to this piece is not familiar, found on the internet, and now forget to find who, if you found out, please tell me your link, I indicate the source
Here is the relevant code:
Package Run.utils;import Java.io.bufferedinputstream;import Java.io.file;import java.io.fileinputstream;import Java.io.fileoutputstream;import Java.util.zip.crc32;import Java.util.zip.checkedoutputstream;import Java.util.zip.zipentry;import Java.util.zip.zipoutputstream;public class Zipcompressor {static final int BUFFER = 8 192; Private File ZipFile; Public Zipcompressor (String pathName) {zipfile = new File (pathName); The public void compress (String ... pathName) {zipoutputstream out = null; try {fileoutputstream FileOutputStream = new FileOutputStream (ZipFile); Checkedoutputstream cos = new Checkedoutputstream (FileOutputStream, New CRC32 ()); out = new Zipoutputstream (COS); String basedir = ""; for (int i=0;i<pathname.length;i++) {Compress (new File (Pathname[i]), out, basedir); } out.close (); } catch (Exception e) {throw new RuntimeException (e); }} public void compress (String srcpathname) { File File = new file (srcpathname); if (!file.exists ()) throw new RuntimeException (Srcpathname + "does not exist!) "); try {fileoutputstream FileOutputStream = new FileOutputStream (ZipFile); Checkedoutputstream cos = new Checkedoutputstream (FileOutputStream, New CRC32 ()); Zipoutputstream out = new Zipoutputstream (COS); String basedir = ""; Compress (file, out, basedir); Out.close (); } catch (Exception e) {throw new RuntimeException (e); }} private void compress (file file, zipoutputstream out, String basedir) {/* Determines whether it is a directory or a file */ if (File.isdirectory ()) {System.out.println ("compressed:" + Basedir + file.getname ()); This.compressdirectory (file, out, basedir); } else {System.out.println ("compressed:" + Basedir + file.getname ()); This.compressfIle (file, out, basedir); }}/** compresses a directory */private void Compressdirectory (File dir, zipoutputstream out, String basedir) { if (!dir.exists ()) return; file[] files = dir.listfiles (); for (int i = 0; i < files.length; i++) {/* recursive */compress (Files[i], out, Basedir + DIR.GETN Ame () + "/"); }}/** compresses a file */private void Compressfile (file file, zipoutputstream out, String basedir) { if (!file.exists ()) {return; } try {Bufferedinputstream bis = new Bufferedinputstream (New Fileinputstre AM (file)); ZipEntry entry = new ZipEntry (Basedir + file.getname ()); Out.putnextentry (entry); int count; byte data[] = new Byte[buffer]; while ((count = bis.read (data, 0, BUFFER))! =-1) {out.write (data, 0, CouNT); } bis.close (); } catch (Exception e) {throw new RuntimeException (e); }} public static void Main (string[] args) {zipcompressor Zc = new Zipcompressor ("C:/users/wj/deskto P/aaa.zip "); Zc.compress ("C:/users/wj/desktop/water.js", "c:/users/wj/desktop/index.jsp"); }}
Java implementation of multiple files of the ZIP package generation