Java implementation of compressed file decompression operation, the disadvantage is that the compressed file can not contain files in Chinese file name, or the following error will occur:
Copy Code code as follows:
Exception in thread "main" Java.lang.IllegalArgumentException:MALFORMED
At java.util.zip.ZipCoder.toString (Unknown Source)
At Java.util.zip.ZipInputStream.readLOC (Unknown Source)
At Java.util.zip.ZipInputStream.getNextEntry (Unknown Source)
At Com.javatest.techzero.gui.ZipFileDemo.main (zipfiledemo.java:22)
Code:
Copy Code code as follows:
Package Com.javatest.techzero.gui;
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.io.InputStream;
Import Java.io.OutputStream;
Import Java.util.zip.ZipEntry;
Import Java.util.zip.ZipFile;
Import Java.util.zip.ZipInputStream;
public class Zipfiledemo {
@SuppressWarnings ("resource")
public static void Main (String args[]) throws Exception {
File File = new file ("D:" + File.separator + "Test.zip");
File outfile = null;
ZipFile zipfile = new ZipFile (file);
Zipinputstream zipinput = new Zipinputstream (new FileInputStream (file));
ZipEntry entry = null;
InputStream input = null;
OutputStream out = null;
while ((entry = Zipinput.getnextentry ())!= null) {
System.out.println ("Start decompression" + entry.getname () +) file ... ");
outfile = new File ("D:" + File.separator + entry.getname ());
if (!outfile.getparentfile (). exists ()) {
Outfile.getparentfile (). mkdir ();
}
if (!outfile.exists ()) {
Outfile.createnewfile ();
}
Input = Zipfile.getinputstream (entry);
out = new FileOutputStream (outfile);
int temp = 0;
while (temp = Input.read ())!=-1) {
<span style= "White-space:pre" > </span>//system.out.println (temp);
Out.write (temp);
}
Input.close ();
Out.close ();
}
System.out.println ("done!");
}
}