The file must be supported by the ant. jar package.
- Package com. amarsoft. CEB. util;
- Import java. Io .*;
- Import java. util. arraylist;
- Import java. util. enumeration;
- Import org.apache.tools.zip. zipentry;
- Import org.apache.tools.zip. zipfile;
- Public class ziptools {
- Private arraylist list = new arraylist (); // stores the extracted file name
- /**
- * Generate the file path
- * 1. If the sub-file is empty and the directory does not exist, create a directory
- * 2. If directory and subdirectory are not empty, press d to create a directory.
- * @ Param directory
- * @ Param subdirectory
- */
- Private void createdirectory (string directory, string subdirectory ){
- String dir [];
- File FL = new file (directory );
- Try {
- If (subdirectory. Equals ("") & FL. exists () = false ){
- Fl. mkdir ();
- } Else if (! Subdirectory. Equals ("")){
- Dir = subdirectory. Replace ('/', '/'). Split ("/");
- For (INT I = 0; I <dir. length; I ++ ){
- File subfile = new file (directory + file. Separator + dir [I]);
- If (subfile. exists () = false) subfile. mkdir ();
- Directory + = file. Separator + dir [I];
- }
- }
- }
- Catch (exception ex ){
- System. Out. println (ex. getmessage ());
- }
- }
- /**
- *
- * @ Param zipfilename ZIP file name
- * @ Param outputdirectory: output directory
- * @ Return array (two elements). The first table indicates the unzipping flag, and the second is the message.
- * @ Throws exception
- */
- Public String [] unzip (string zipfilename, string outputdirectory ){
- String [] sreturn = new string [2];
- Boolean readflag = false; // read the file ID to prevent Empty files from being decompressed.
- Try {
- Zipfile = new zipfile (zipfilename );
- Enumeration E = zipfile. getentries ();
- Zipentry = NULL;
- Createdirectory (outputdirectory, ""); // generate the output directory
- While (E. hasmoreelements () {// if there is a file in the compressed file, decompress
- String upzipedfilename;
- Readflag = true;
- Zipentry = (zipentry) E. nextelement ();
- Upzipedfilename = zipentry. getname ();
- System. Out. println ("unziping:" + upzipedfilename );
- List. Add (upzipedfilename); // put the file name in a list for return calls.
- If (zipentry. isdirectory () {// if it is a directory, create it
- String name = zipentry. getname ();
- Name = Name. substring (0, name. Length ()-1 );
- File F = new file (outputdirectory + file. Separator + name );
- F. mkdir ();
- System. Out. println ("create directory:" + outputdirectory + file. Separator + name );
- } Else {
- String filename = zipentry. getname ();
- Filename = filename. Replace ('//','/');
- If (filename. indexof ("/")! =-1 ){
- Createdirectory (outputdirectory, filename. substring (0, filename. lastindexof ("/")));
- Filename = filename. substring (filename. lastindexof ("/") + 1, filename. Length ());
- }
- 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 ();
- }
- }
- If (readflag) {// determine whether there are files in the zip package
- Sreturn [0] = "1 ";
- Sreturn [1] = "successful ";
- } Else {
- Sreturn [0] = "0 ";
- Sreturn [1] = "no file exists in the zip package ";
- }
- } Catch (exception ex ){
- Sreturn [0] = "0 ";
- Sreturn [1] = ex. getmessage ();
- }
- Return sreturn;
- }
- Public arraylist getfilenames (){
- Return list;
- }
- ///**
- // * Test Method
- // * @ Param ARGs
- //*/
- // Public static void main (string [] ARGs ){
- // String szipfilename = "D: // parttest.zip ";
- // String sdirectory = "D: // tmp ";
- // String sflag = "";
- // String MSG = "";
- // String [] sreturn;
- // Ziptools DBC = new ziptools ();
- // System. Out. println ("message: start extracting ......");
- // Sreturn = DBC. Unzip (szipfilename, sdirectory );
- // Sflag = sreturn [0];
- // MSG = sreturn [1];
- // If (sflag. Equals ("1 ")){
- // System. Out. println ("message: decompressed successfully ");
- //} Else if (sflag. Equals ("0 ")){
- // System. Out. println ("message: Decompression failed ");
- // System. Out. println ("Reason:" + MSG );
- //}
- //
- //}
- }