Java file compression and decompression

Source: Internet
Author: User

Feature implementation.

 Packagecom.test;ImportJava.io.File;ImportJava.io.BufferedOutputStream;ImportJava.io.BufferedInputStream;ImportJava.io.FileOutputStream;ImportJava.io.FileInputStream;ImportJava.util.zip.ZipEntry;ImportJava.util.zip.ZipInputStream;ImportJava.util.zip.ZipOutputStream;/*** This class implements folders compressed into Zip files and zip files extracted * *@authorAdministrator **/ Public classZip {PrivateZipinputstream Zipin;//Unzip zip    PrivateZipoutputstream zipout;//Compress zip    PrivateZipEntry ZipEntry; Private Static intBufSize;//size of bytes    Private byte[] buf; Private intreadedbytes;  PublicZip () { This(512); }     PublicZip (intbufSize) {         This. bufSize =bufSize;  This. BUF =New byte[ This. BufSize]; }    //compress files within a folder     Public voidDozip (String zipdirectory) {//Zipdirectorypath: folder name that needs to be compressedfile file;        File Zipdir; Zipdir=NewFile (zipdirectory); String Zipfilename= Zipdirectory + ". zip";//zip file name generated after compression        Try {             This. ZipOut =NewZipoutputstream (NewBufferedoutputstream (NewFileOutputStream (zipfilename)); Handledir (Zipdir, This. ZipOut);  This. Zipout.close (); } Catch(Exception IoE) {ioe.printstacktrace (); }    }    //called by Dozip to complete the directory file read recursively    Private voidHandledir (File dir, Zipoutputstream zipout)throwsException {FileInputStream Filein;        file[] files; Files=Dir.listfiles (); if(Files.length = = 0) {//If the directory is empty, it is created separately. //in the ZipEntry isdirectory () method, the directory ends with "/".             This. Zipout.putnextentry (NewZipEntry (dir.tostring () + "/"));  This. Zipout.closeentry (); } Else{//If the directory is not empty, the directories and files are processed separately.             for(File filename:files) {if(Filename.isdirectory ()) {Handledir (FileName, This. ZipOut); } Else{Filein=NewFileInputStream (fileName); String name=Dir.getname (); //The resulting compressed package is stored in the original directory                     This. Zipout.putnextentry (NewZipEntry (name + "/" +filename.getname (). toString ()); //This method is stored in the project directory//This.zipOut.putNextEntry (New//ZipEntry (filename.tostring ()));                     while(( This. readedbytes = Filein.read ( This. buf)) > 0) {                         This. Zipout.write ( This. buf, 0, This. readedbytes); }                     This. Zipout.closeentry (); }            }        }    }    //unzip the specified zip file     Public voidUnZip (String unzipfilename) {//unzipfilename the zip file name to unzipFileOutputStream fileout;        File file; String F= unzipfilename.substring (0, Unzipfilename.length ()-4); File FF=NewFile (f); Try {             This. Zipin =NewZipinputstream (NewBufferedinputstream (NewFileInputStream (unzipfilename));  while(( This. ZipEntry = This. Zipin.getnextentry ())! =NULL) {file=NewFile ( This. Zipentry.getname ()); if( This. Zipentry.isdirectory ())                {File.mkdirs (); } Else {                    //if the directory for the specified file does not exist, it is created.File parent =File.getparentfile (); if(!parent.exists ())                    {Parent.mkdirs (); }                    if(!ff.exists ())                    {Ff.mkdir (); } fileout=NewFileOutputStream (f + "/" +file.getname ()); //fileout = new FileOutputStream (file); This method is stored in the project directory                     while(( This. readedbytes = This. Zipin.read ( This. buf)) > 0) {Fileout.write ( This. buf, 0, This. readedbytes);                } fileout.close (); }                 This. Zipin.closeentry (); }        } Catch(Exception IoE) {ioe.printstacktrace (); }    }    //Set Buffer size     Public voidSetbufsize (intbufSize) {         This. bufSize =bufSize; }    //Test Zip class     Public Static voidMain (string[] args)throwsException {Zip zip=NewZip (); Zip.dozip ("E:\\test");//zip.unzip ("C:\\test.zip");    }}

Java file compression and decompression

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.