Java implementation zip compressed file (multiple folders in the same folder is typed into a zip package)

Source: Internet
Author: User
Tags cos crc32

Reference: http://blog.163.com/shanqing_shuixiu@yeah/blog/static/165319229201192444434865/

Talk is cheap,show me the code!


Multiple folders under the same folder to make a ZIP package


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 = 8192;                private File zipFile;                 public zipcompressor (String pathname)  {              zipfile = new file (PathName);           }          public void compress (String... pathName)  {            ZipOutputStream out =  null;              try {                  FileOutputStream  Fileoutputstream = new fileoutputstream (ZipFile);                   CheckedOutputStream cos = new  Checkedoutputstream (fileoutputstream,              &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;NEW&NBSP;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,               &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;NEW&NBSP;CRC32 ());                   zipoutputstream out  = new zipoutputstream (COS);                   String basedir =  "";                   compress (File, out, basedir);                    Out.close ();              } catch  ( Exception e)  {                   throw&nbsP;new runtimeexception (e);              }           }               private void compress (file file, zipoutputstream out,  String basedir)  {              /*   Determine whether the directory or file  */             if  ( File.isdirectory ())  {                   system.out.println ("Compression:"  + basedir + file.getname ());                   this.compressdirectory ( File, out, basedir);              }  else {                   System.out.println ("Compression:"  + basedir + file.getname ());                   this.compressfile (file, out,  Basedir);              }           }                /**  Compress 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++)  {                   /*  recursion  */                  compress (files[i], out, basedir +  Dir.getname ()  +  "/");     

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.