我写了一个java zip解压程序,但是在解压有时会产生中文乱码,我已经用了ant库了,但是还是会产生中文乱码,导致程序报错,代码如下:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
public class Test2 {
public static void main(String[] args) throws IOException {
File f1 = new File("f:/");
ZipFile zf = new ZipFile("f:/手册.zip");
Enumeration<ZipEntry> e = (Enumeration<ZipEntry>) zf.getEntries();
while(e.hasMoreElements()){
ZipEntry en = e.nextElement();
File f2 = new File(f1.getPath()+"/"+en.getName());
System.out.println(en.getName()+"\t"+f2.getPath());
if(en.isDirectory()){
f2.mkdir();
System.out.println(f2.getPath());
} else {
f2.getParentFile().mkdirs();
f2.createNewFile();
InputStream in = zf.getInputStream(en);
OutputStream os = new FileOutputStream(f2);
byte[] buffer = new byte[1024];
int n = 0;
while((n=in.read(buffer))>0){
os.write(buffer, 0, n);
}
System.out.println(f2.getPath());
os.close();
in.close();
}
}
}
}
请各位高手帮忙看一下,哪里有问题。
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
public class Test2 {
public static void main(String[] args) throws IOException {
File f1 = new File("f:/");
ZipFile zf = new ZipFile("f:/手册.zip");
Enumeration<ZipEntry> e = (Enumeration<ZipEntry>) zf.getEntries();
while(e.hasMoreElements()){
ZipEntry en = e.nextElement();
File f2 = new File(f1.getPath()+"/"+en.getName());
System.out.println(en.getName()+"\t"+f2.getPath());
if(en.isDirectory()){
f2.mkdir();
System.out.println(f2.getPath());
} else {
f2.getParentFile().mkdirs();
f2.createNewFile();
InputStream in = zf.getInputStream(en);
OutputStream os = new FileOutputStream(f2);
byte[] buffer = new byte[1024];
int n = 0;
while((n=in.read(buffer))>0){
os.write(buffer, 0, n);
}
System.out.println(f2.getPath());
os.close();
in.close();
}
}
}
}
请各位高手帮忙看一下,哪里有问题。
如何修改我也不会,去研究一下java的zip流估计就行了。