Java 文件解压缩实现代码
java实现压缩文件的解压缩操作,缺点是压缩文件内不能含有文件名为中文的的文件,否则会出现如下错误:
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)
代码:
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("开始解压缩" + entry.getname() + "文件。。。");
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!");
}
}