Java implementation of multiple files of the ZIP package generation

Source: Internet
Author: User
Tags cos crc32

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.