/*
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;
}
}