Get started with the java.util.zip package in extracting files

Source: Internet
Author: User

Get started with the java.util.zip package in extracting files
Get started with the java.util.zip package in extracting files
I accidentally clicked some of the source code in the java.util.zip package. Knowing the scope of use is for our daily
File compression and decompression. Write some common compressed files (compression of a single file and multiple files), decompress files (decompress a single file and folder)
Demo.

Package com.clark.zip;


Import java. io. File;
Import java. io. FileInputStream;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import java. io. InputStream;
Import java. io. OutputStream;
Import java.util.zip. ZipEntry;
Import java.util.zip. ZipException;
Import java.util.zip. ZipFile;
Import java.util.zip. ZipInputStream;
Import java.util.zip. ZipOutputStream;


/**
* Single file Compression
* @ Author caolipeng
* @ Date 2:48:15, January 1, December 22, 2014
* @ SingleFileZip. java
* @ Version 1.0
*/
Public class FileZip {

Public static void main (String [] args ){
/* GetZipFileName ();*/
/** Compress a single file */
/* String filepath = "F:/iBoxClearDB_Orcl. SQL ";
String zippath = "F:/test/db.zip ";
ZipFile (filepath, zippath );*/
/** Compress multiple files at a time and store the files in a folder */
/* String filepath = "E:/weekly /";
String zippath = "F:/test/week-job.zip ";
ZipMultiFile (filepath, zippath );*/
/** Extract (extract a single file )*/
/* String zippath = "F:/test/db.zip ";
String outfilepath = "F:/test. SQL ";
String filename = "iBoxClearDB_Orcl. SQL ";
ZipContraFile (zippath, outfilepath, filename );*/
/** Decompress (the compressed file contains multiple files )*/
String zippath = "F:/test/week-job.zip ";
String outfilepath = "F :/";
ZipContraMultiFile (zippath, outfilepath );
}

/** Get the zip file name */
Private static void getZipFileName (){
File file = new File ("F:/test/temp.zip ");
ZipFile zipFile = null;
Try {
ZipFile = new ZipFile (file );
System. out. println ("zip file name is:" + zipFile. getName ());
} Catch (ZipException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
} Finally {
If (zipFile! = Null ){
Try {
ZipFile. close ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
}
}

/** Compress a single file */
Public static void zipFile (String filepath, String zippath ){
InputStream input = null;
ZipOutputStream zipOut = null;
Try {
File file = new File (filepath );
File zipFile = new File (zippath );
Input = new FileInputStream (file );
ZipOut = new ZipOutputStream (new FileOutputStream (zipFile ));
ZipOut. putNextEntry (new ZipEntry (file. getName ()));
Int temp = 0;
While (temp = input. read ())! =-1 ){
ZipOut. write (temp );
}
System. out. println ("zip" + filepath + "to" + zippath );
} Catch (Exception e ){
E. printStackTrace ();
} Finally {
Try {
Input. close ();
ZipOut. close ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
}

/** Compress multiple files at a time and store the files in a folder */
Public static void zipMultiFile (String filepath, String zippath ){
Try {
File file = new File (filepath); // folder to be compressed
File zipFile = new File (zippath );
InputStream input = null;
ZipOutputStream zipOut = new ZipOutputStream (new FileOutputStream (zipFile ));
If (file. isDirectory ()){
File [] files = file. listFiles ();
For (int I = 0; I <files. length; ++ I ){
Input = new FileInputStream (files [I]);
ZipOut. putNextEntry (new ZipEntry (file. getName () + File. separator + files [I]. getName ()));
Int temp = 0;
While (temp = input. read ())! =-1 ){
ZipOut. write (temp );
}
Input. close ();
}
} Else {// otherwise, the method for compressing a single file is called.
ZipFile (filepath, zippath );
}
ZipOut. close ();
System. out. println ("zip directory is success ");
} Catch (Exception e ){
E. printStackTrace ();
}
}

/** Extract (extract a single file )*/
Public static void zipContraFile (String zippath, String outfilepath, String filename ){
Try {
File file = new File (zippath); // compressed File path and file name
File outFile = new File (outfilepath); // path and File name after decompression
ZipFile zipFile = new ZipFile (file );
ZipEntry entry = zipFile. getEntry (filename); // The extracted file name.
InputStream input = zipFile. getInputStream (entry );
OutputStream output = new FileOutputStream (outFile );
Int temp = 0;
While (temp = input. read ())! =-1 ){
Output. write (temp );
}
Input. close ();
Output. close ();
} Catch (Exception e ){
E. printStackTrace ();
}
}

/** Extract (the compressed file contains multiple files) instead of the preceding method.
* ZipInputStream class
* ZipEntry cannot be used when we need to extract multiple files,
* To perform operations on more complex compressed files, we must use the ZipInputStream class.
**/
Public static void ZipContraMultiFile (String zippath, String outzippath ){
ZipInputStream zipInput = null;
ZipFile zipFile = null;
InputStream input = null;
OutputStream output = null;
Try {
File file = new File (zippath );
File outFile = null;
ZipFile = new ZipFile (file );
ZipInput = new ZipInputStream (new FileInputStream (file ));
ZipEntry entry = null;
While (entry = zipInput. getNextEntry ())! = Null ){
System. out. println ("decompress" + entry. getName () + "file ");
OutFile = new File (outzippath + File. separator + entry. getName ());
If (! OutFile. getParentFile (). exists ()){
OutFile. getParentFile (). mkdir ();
}
If (! OutFile. exists ()){
OutFile. createNewFile ();
}
Input = zipFile. getInputStream (entry );
Output = new FileOutputStream (outFile );
Int temp = 0;
While (temp = input. read ())! =-1 ){
Output. write (temp );
}
}
} Catch (Exception e ){
E. printStackTrace ();
} Finally {
Try {
Input. close ();
Output. close ();
ZipInput. close ();
ZipFile. close ();
} Catch (IOException e ){
E. printStackTrace ();
}
}
}
}

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.