使用java.util.zip下的类进行zip压缩,它使用的是uft-8的编码方式,这样会引起中文名变成乱码的情况,解决的方法是用truezip.jar下的类,truezip的相关介绍和下载见[url]https://truezip.dev.java.net/[/url]
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.CopyUtils;
import de.schlichtherle.io.FileInputStream;
import de.schlichtherle.io.FileOutputStream;
import de.schlichtherle.util.zip.ZipEntry;
import de.schlichtherle.util.zip.ZipOutputStream;
public class Test
{
public static void main(String []args); throws Exception
{
String needtozipfilepath ="";
String zipfilepath = "";
File needtozipfile = new File(needtozipfilepath);;
File zipfile = new File(zipfilepath);;
FileOutputStream fout = new FileOutputStream(zipfile);;
ZipOutputStream zout = new ZipOutputStream(fout,"GBK");; //解决中文问题的关键所在
try
{
for(File in : needtozipfile.listFiles(););
{
ZipEntry ze = new ZipEntry(in.getName(););;
zout.putNextEntry(ze);;
FileInputStream fis = new FileInputStream(in);;
try
{
CopyUtils.copy(fis,zout);;
}
catch (IOException e);
{
// TODO: handle exception
}
finally
{
if(fis != null);
fis.close();;
zout.closeEntry();;
}
}
}
catch (IOException e);
{
// TODO: handle exception
}
finally
{
if(zout != null);
{
zout.close();;
fout.close();;
}
}
}
}