1: Test the compression of a single file.
Public void zipfile () throws ioexception {
// Name of the file generated after compression
Zipoutputstream ZOS = new zipoutputstream (New fileoutputstream ("F: \ testzip.zip "));
// Instantiate a zipentry object and compress the test.txt File
Zipentry entry = new zipentry ("test.txt ");
ZOS. setcomment ("Zip test compressing a single file ");
ZOS. putnextentry (entry );
Inputstream is = new fileinputstream ("F: \ test.txt ");
Int Len = 0;
While (LEN = is. Read ())! =-1)
ZOS. Write (LEN );
Is. Close ();
ZOS. Close ();
}
2: Test to compress multiple files. After compression, use the decompression software to open the compressed folder.
Public void zipmufile () throws ioexception {
// The folder to be compressed
File infile = new file ("F: \ testdir \\");
// Zip compressed file generated after compression
Zipoutputstream ZOS = new zipoutputstream (New fileoutputstream ("F: \ testdir.zip "));
ZOS. setcomment ("Multi-file processing ");
Zipmyfile (infile, ZOS, ""); // function call, as shown below:
ZOS. Close ();
}
Public void zipmyfile (File infile, zipoutputstream ZOS, string DIR) throws ioexception {
If (infile. isdirectory ()){
File [] files = infile. listfiles ();
For (File file: files)
Zipfilemy (file, ZOS, Dir + "\" + infile. getname ());
} Else {
String entryname = NULL;
If (! "". Equals (DIR ))
Entryname = dir + "\" + infile. getname ();
Else
Entryname = infile. getname ();
Zipentry entry = new zipentry (entryname );
ZOS. putnextentry (entry );
Inputstream is = new fileinputstream (infile );
Int Len = 0;
While (LEN = is. Read ())! =-1)
ZOS. Write (LEN );
Is. Close ();
}
}
3: compress the folder and open the files in the folder instead of the directory.
Public void compressfileanddir () throws ioexception {
// The Directory to be compressed. However, there can only be one directory under this directory. If there is a directory under this directory, you can use recursive methods.
String srcfile = "F: \ HTML5 ";
// Compressed, compressed file
String zipfile = "F: \ html5.zip ";
// File zipfile = new file (zipfilepath );
File file = new file (srcfile );
File [] srcfiles = file. listfiles ();
Fileoutputstream = new fileoutputstream (zipfile );
Zipoutputstream out = new zipoutputstream (fileoutputstream );
For (INT I = 0; I <srcfiles. length; I ++ ){
If (srcfiles [I]. isdirectory ()){
File [] files = srcfiles [I]. listfiles ();
For (Int J = 0; j <files. length; j ++ ){
Comprefile (files [J], srcfiles [I]. getname () + "/", out); // method call, as shown below:
}
} Else {
Comprefile (srcfiles [I], "", out); // method call
}
}
Out. Flush ();
Out. Close ();
}
Public void comprefile (File file, string Dir, zipoutputstream out ){
Byte [] Buf = new byte [1, 1024];
Try {
// Compress the files
Fileinputstream in = new fileinputstream (File );
Out. putnextentry (New zipentry (DIR + file. getname ()));
Int Len;
While (LEN = in. Read (BUF)> 0 ){
Out. Write (BUF, 0, Len );
}
Out. closeentry ();
In. Close ();
} Catch (exception e ){
// Todo: handle exception
E. printstacktrace ();
}
}
4: extract and decompress the file, which is relatively simple.
Public void unzip () throws zipexception, ioexception {
System. Out. println ("start to decompress the file .... ");
File file = new file ("F: \ yingtao.zip"); // The compressed file to be decompressed.
Zipfile = new zipfile (File );
Zipinputstream = new zipinputstream (New fileinputstream (File ));
Zipentry = NULL;
While (zipentry = zipinputstream. getnextentry ())! = NULL ){
String filename = zipentry. getname ();
File temp = new file ("F: \ unzip \" + filename); // decompressed file path
If (! Temp. getparentfile (). exists ())
Temp. getparentfile (). mkdirs ();
Outputstream OS = new fileoutputstream (temp );
Inputstream is = zipfile. getinputstream (zipentry );
Int Len = 0;
While (LEN = is. Read ())! =-1)
OS. Write (LEN );
OS. Close ();
Is. Close ();
}
Zipinputstream. Close ();
}
Here I am leaving QQ: 693614965. If you have any questions, we can communicate with each other.