Java decompress ZIP files

Source: Internet
Author: User
Tags decompress zip

Keywords: Decompress the ZIP file

Even though this problem occurs, you need to decompress the ZIP file and read the data of the. txt and. INF files in the compressed package. After JDK helper and Google, the problem can be solved. Here, I only want to understand the pressure process. In the main method, the compressed package storage path and the file to which it is compressed are hardcoded.ProgramThe overall structure is not very satisfactory. We still need to refactor it .......

Package com. Da. Unzip;

Import java. Io. bufferedinputstream;
Import java. Io. bufferedoutputstream;
Import java. Io. bufferedreader;
Import java. Io. file;
Import java. Io. fileinputstream;
Import java. Io. filenotfoundexception;
Import java. Io. fileoutputstream;
Import java. Io. filereader;
Import java. Io. ioexception;
Import java. Io. inputstream;
Import java. Io. inputstreamreader;
Import java. Io. outputstream;
Import java. Io. reader;
Import java. NiO. bytebuffer;
Import java. util. arraylist;
Import java. util. enumeration;
Import java. util. List;
Import java.util.zip. zipentry;
Import java.util.zip. zipfile;

Public class unzip {

Public static void main (string [] ARGs) throws exception {

Unzip unzip = new unzip ();
String zippath = "C :\\ unzip \"; // path of the target file to which the package is extracted
String zipdir = "C: \ data \"; // storage path of the compressed file to be decompressed

File file = new file (zipdir );
List list = unzip. getsubfiles (File );
For (Object OBJ: List ){
String realname = (File) OBJ). getname ();
System. Out. println (realname );
Int end = realname. lastindexof (".");
System. Out. println ("file name to be decompressed..." + zipdir + realname );
System. Out. println ("decompressed directory" + zippath + realname. substring (0, end ));
Unzip. testreadzip (zippath, zipdir + realname );
}

}

 

/*
* Extract the zippath directory file to the unzippath directory. @ throws exception
*/
Public void readzip (string zippath, string unzippath) throws exception {

Zipfile zfile = new zipfile (unzippath); // generate a zip file object

System. Out. println (zfile. getname (); // obtain the full path of the ZIP file to be decompressed

Enumeration zlist = zfile. Entries (); // return the enumerated object

Zipentry ze = NULL; // used to indicate ZIP file entries

Byte [] Buf = new byte [1024]; // declare a byte array
/**
* Cyclically retrieve every file in the ZIP file
*/
While (zlist. hasmoreelements ()){
// Obtain a zipentry from zipfile
Ze = (zipentry) zlist. nextelement ();
If (ze. isdirectory () // if it is a directory entry, true is returned and the following statement is executed
{
System. Out. println ("dir:" + Ze. getname () + "skipped ..");
Continue;
}
Int begin = zfile. getname (). lastindexof ("\") + 1;
Int end = zfile. getname (). lastindexof (".");
String ziprealname = zfile. getname (). substring (begin, end );
System. out. println ("extract start extracting:" + Ze. getname () + "\ t" + Ze. getsize () + "\ t" + Ze. getcompressedsize ());
// Use zipentry as the parameter to get an inputstream, write it to outputstream, and add the buffer
Outputstream OS = new bufferedoutputstream (
New fileoutputstream (getrealfilename (zippath + "\\"
+ Ziprealname, ze. getname ())));
Inputstream is = new bufferedinputstream (zfile. getinputstream (ze ));
String filename = getrealfilename (zippath, ze. getname (). getname ();
System. Out. println ("Name of the decompressed file:" + filename );
Int readlen = 0;
While (readlen = is. Read (BUF, 0, 1024 ))! =-1 ){
OS. Write (BUF, 0, readlen );
}
Is. Close ();
OS. Close ();
// System. Out. println ("decompress end extracted:" + Ze. getname ());
}
Zfile. Close ();
}

/**
* If the root directory is specified, the actual file name corresponding to a relative path is returned.
*
* @ Param zippath
* Specify the root directory
* @ Param absfilename
* Relative Path Name, from name in zipentry
* @ Return java. Io. File actual file
*/
Private file getrealfilename (string zippath, string absfilename ){

String [] dirs = absfilename. Split ("/", absfilename. Length ());

File ret = new file (zippath); // create a file object

If (dirs. length> 1 ){
For (INT I = 0; I <dirs. Length-1; I ++ ){
Ret = new file (Ret, dirs [I]);

}
}

If (! Ret. exists () {// check whether the file exists
Ret. mkdirs (); // create the directory specified by this abstract path name
}
Ret = new file (Ret, dirs [dirs. Length-1]); // abstract the path name and child according to ret
// Create a new file instance using the path name string

Return ret;
}
 
}

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.