Java decompress ZIP

Source: Internet
Author: User
Tags decompress zip

/*
Provides the ZIP file decompression interface:
Adapterzipfile:
Input: zipfilename (absolute path of the ZIP file) and outputdirectory (storage path of the ZIP file after decompression)
Output:
Description: initialization Function
Unzipfile:
Input: None
Output:
Description: Decompress the ZIP file and store it in the outputdirectory path.
Getzipfilelist:
Input: None
Output: List
Note: the absolute path of the decompressed file list is stored in the arraylist dynamic array.
*/

Package com.mop.pushmail.adapter.html ziprar;

Import java. Io .*;
Import java. util .*;
Import org.apache.tools.zip .*;

Public class adapterzipfile {
String zipfilename = NULL; // absolute path of the ZIP file
String outputdirectory = NULL; // storage path of the ZIP file after decompression
Arraylist list = new arraylist (); // absolute path of the extracted file list

// Public adapterzipfile (string zipfilename ){
// If (zipfilename = NULL ){
// Return;
//}
// This.zip filename = zipfilename. Replace ("/","//");
// Unzipfile ();
//}

// Initialization
Public adapterzipfile (string zipfilename, string outputdirectory ){
If (zipfilename = NULL ){
Return;
}
This.zip filename = zipfilename;
If (outputdirectory! = NULL ){
This. outputdirectory = outputdirectory;
} Else {
This. outputdirectory = zipfilename. substring (0,
Zipfilename. Replace ('//', '/'). lastindexof ("/"));
}
// Unzipfile ();
}

// Create a directory
Private void createdirectory (string directory, string subdirectory ){
String dir [];
File FL = new file (directory );
Try {
If (subdirectory = "" & FL. exists ()! = True)
Fl. mkdirs ();
Else if (subdirectory! = ""){
Dir = subdirectory. Replace ("//", "/"). Split ("/");
For (INT I = 0; I <dir. length; I ++ ){
File subfile = new file (directory + file. Separator + dir [I]);
If (! Subfile. exists ())
Subfile. mkdir ();
Directory + = file. Separator + dir [I];
}
}
} Catch (exception ex ){
System. Out. println (ex. getmessage ());
}
}

// Decompress the file
// If outputdierctory is null, decompress it to the current directory of the zipfilename file.
Public void unzipfile (){
File file = new file (zipfilename );
If (! File. exists ()){
Return;
}
Createdirectory (outputdirectory ,"");

Try {
Zipfile = new zipfile (zipfilename );
Enumeration E = zipfile. getentries ();
Zipentry = NULL;
While (E. hasmoreelements ()){
Zipentry = (zipentry) E. nextelement ();
If (zipentry. isdirectory ()){
String name = zipentry. getname ();
Name = Name. substring (0, name. Length ()-1 );
File F = new file (outputdirectory + file. Separator + name );
F. mkdir ();
} Else {
String filename = zipentry. getname ();
Filename = filename. Replace ('//','/');
// Create a directory under the multi-level directory, for example, D1/D2/1.txt.
If (filename. indexof ("/")! =-1 ){
Createdirectory (outputdirectory, filename. substring (0,
Filename. lastindexof ("/")));
Filename = filename. substring (
Filename. lastindexof ("/") + 1, filename
. Length ());
}

List. Add (outputdirectory + file. Separator
+ Zipentry. getname ());
File F = new file (outputdirectory + file. Separator
+ Zipentry. getname ());

F. createnewfile ();
Inputstream in = zipfile. getinputstream (zipentry );
Fileoutputstream out = new fileoutputstream (f );

Byte [] by = new byte [1024];
Int C;
While (C = in. Read ())! =-1 ){
Out. Write (by, 0, C );
}
Out. Close ();
In. Close ();
}
}
} Catch (exception ex ){
System. Out. println (ex. getmessage ());
}
}

// Return the absolute path of the decompressed ZIP file list
Public arraylist getzipfilelist (){
Return list;
}
}

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.