Document directory
- Zipfile
- Compressing a single file
- Compressing multiple files (folders)
- Decompress a single file
- Decompress multiple files
The import java.util.zip. * package in Java API contains all Java operations on compressed files.
We can use the methods in this package to perform File compression and decompression operations based on the relevant knowledge in Io.
Each compressed file in zipfilejava can be represented by zipfile.
File file = new file ("F:/zippath.zip"); zipfile = new zipfile (File); system. out. println ("compressed file name:" + zipfile. getname ());
Compressing a single file
/** Compress a single file */public static void zipfile (string filepath, string zippath) {try {file = new file (filepath); file zipfile = new file (zippath ); inputstream input = new fileinputstream (File); zipoutputstream zipout = new zipoutputstream (New fileoutputstream (zipfile); zipout. putnextentry (New zipentry (file. getname (); int temp = 0; while (temp = input. read ())! =-1) {zipout. Write (temp);} input. Close (); zipout. Close ();} catch (exception e) {e. printstacktrace ();}}
Application:
ZipFile("d:/hello.txt", "d:/hello.zip");
Compressing multiple files (folders)
/** Compress multiple files at a time and store the files in a folder */public static void zipmultifile (string filepath, string zippath) {try {file = new file (filepath ); // file zipfile = new file (zippath); inputstream input = NULL; zipoutputstream zipout = new zipoutputstream (New fileoutputstream (zipfile); If (file. isdirectory () {file [] files = file. listfiles (); For (INT I = 0; I <files. length; ++ I) {input = new fileinputstre Am (files [I]); zipout. putnextentry (New zipentry (file. getname () + file. separator + files [I]. getname (); int temp = 0; while (temp = input. read ())! =-1) {zipout. write (temp);} input. close () ;}} zipout. close ();} catch (exception e) {e. printstacktrace ();}}
Application:
ZipMultiFile("f:/uu", "f:/zippath.zip");
Decompress a single file
/** Decompress (extract a single file) */public static void zipcontrafile (string zippath, string outfilepath, string filename) {try {file = new file (zippath ); // compressed file path and file name OUTFILE = new file (outfilepath); // decompress the file path and file name zipfile = new zipfile (File); zipentry entry = zipfile. getentry (filename); // The extracted file name is inputstream input = zipfile. getinputstream (entry); outputstream output = new fileoutputstream (OUTFILE); In T temp = 0; while (temp = input. Read ())! =-1) {output. Write (temp);} input. Close (); output. Close ();} catch (exception e) {e. printstacktrace ();}}
Application:
ZipContraFile("d:/hello.zip","d:/eee.txt", "hello.txt");
Decompress multiple files in the zipinputstream class: zipentry cannot be used when multiple files need to be decompressed. To operate more complex compressed files, we must use the zipinputstream class.
/** Extract (the compressed file contains multiple files) instead of the preceding method. * Zipinputstream class * zipentry cannot be used when multiple files need to be decompressed. * to perform operations on more complex compressed files, we must use the zipinputstream class **/public static void zipcontramultifile (string zippath, string outzippath) {try {file = new file (zippath); file OUTFILE = NULL; zipfile = new zipfile (File); zipinputstream zipinput = new zipinputstream (New fileinputstream (File); zipentry entry = NULL; inputstream input = NULL; outputstream Output = NULL; while (Entry = zipinput. getnextentry ())! = NULL) {system. out. println ("extract" + entry. getname () + "file"); OUTFILE = new file (outzippath + file. separator + entry. getname (); If (! OUTFILE. getparentfile (). exists () {OUTFILE. getparentfile (). mkdir ();} If (! OUTFILE. exists () {OUTFILE. createnewfile ();} input = zipfile. getinputstream (entry); Output = new fileoutputstream (OUTFILE); int temp = 0; while (temp = input. read ())! =-1) {output. write (temp);} input. close (); output. close () ;}} catch (exception e) {e. printstacktrace ();}}
Application:
ZipContraMultiFile("f:/zippath.zip", "d:/"); ZipContraMultiFile("d:/hello.zip", "d:/");