JDK中自带的ZipOutputStream在压缩文件时,如果文件名中有中文,则压缩后的
zip文件打开时发现中文文件名变成乱码.
解决的方法是使用apache-ant-zip.jar包(见附件)中的ZipOutputStream和ZipEntry.
即,导入类:
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
windows系统环境下
并且注意,压缩之前调用ZipOutputStream的out.setEncoding(System.getProperty("sun.jnu.encoding"));方法,
系统参数sun.jnu.encoding表示获取当前系统中的文件名的编码方式.这里将ZipOutputStream的文件名编码方式
设置成系统的文件名编码方式.
linux环境下
ZipOutputStream的out.setEncoding("GBK");
ZipEntry的 entry.setUnixMode(644);
解压时,直接使用JDK原来的ZipInputStream即可.
但是有个需要注意的地方是,在读取ZIP文件之前,需要设置:
System.setProperty("sun.zip.encoding", System.getProperty("sun.jnu.encoding"));
将系统的ZIP编码格式设置为系统文件名编码方式,否则解压时报异常.
public static voidzip(String srcPathName, String zipFileName)
{
File file= newFile(srcPathName);
File zipFile= newFile(zipFileName);if (!file.exists()) throw new RuntimeException(srcPathName + "不存在!");try{
FileOutputStream fileOutputStream= newFileOutputStream(zipFile);
CheckedOutputStream cos= new CheckedOutputStream(fileOutputStream, newCRC32());
ZipOutputStream out= newZipOutputStream(cos);
Properties pro=System.getProperties();
String osName=pro.getProperty("os.name");if("Linux".equals(osName)||"linux".equals(osName)){
out.setEncoding("GBK");//设置文件名编码方式
}else{
out.setEncoding(System.getProperty("sun.jnu.encoding"));//设置文件名编码方式
}
String basedir= "";
compress(file, out, basedir);
out.close();
}catch(Exception e)
{throw newRuntimeException(e);
}
}
/** inputFileName 输入一个文件夹
* zipFileName 输出一个压缩文件夹*/
private static voidcompress(File file, ZipOutputStream out, String basedir)
{/*判断是目录还是文件*/
if(file.isDirectory())
{//System.out.println("压缩:" + basedir + file.getName());
compressDirectory(file, out, basedir);
}else{//System.out.println("压缩:" + basedir + file.getName());
compressFile(file, out, basedir);
}
}/**压缩一个目录*/
private static voidcompressDirectory(File dir, ZipOutputStream out, String basedir)
{if (!dir.exists()) return;
File[] files=dir.listFiles();for (int i = 0; i < files.length; i++)
{/*递归*/compress(files[i], out, basedir+ dir.getName() + "/");
}
}/**压缩一个文件*/
private static voidcompressFile(File file, ZipOutputStream out, String basedir)
{if (!file.exists())
{return;
}try{
BufferedInputStream bis= new BufferedInputStream(newFileInputStream(file));
ZipEntry entry= new ZipEntry(basedir +file.getName());
Properties pro=System.getProperties();
String osName=pro.getProperty("os.name");if("Linux".equals(osName)||"linux".equals(osName)){
entry.setUnixMode(644);
}
out.putNextEntry(entry);intcount;byte data[] = new byte[8192];while ((count = bis.read(data, 0, 8192)) != -1)
{
out.write(data,0, count);
}
bis.close();
}catch(Exception e)
{throw newRuntimeException(e);
}
}
解压缩使用UnZip代码:
packageio;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.util.zip.ZipEntry;importjava.util.zip.ZipInputStream;public classUnZip
{public static voidunzip(String zipFilePath, String destDir)
{
System.setProperty("sun.zip.encoding", System.getProperty("sun.jnu.encoding")); //防止文件名中有中文时出错//System.out.println(System.getProperty("sun.zip.encoding"));//ZIP编码方式//System.out.println(System.getProperty("sun.jnu.encoding"));//当前文件编码方式//System.out.println(System.getProperty("file.encoding"));//这个是当前文件内容编码方式
File dir= newFile(destDir);//create output directory if it doesn't exist
if (!dir.exists()) dir.mkdirs();
FileInputStream fis;//buffer for read and write data to file
byte[] buffer = new byte[1024];try{
fis= newFileInputStream(zipFilePath);
ZipInputStream zis= newZipInputStream(fis);
ZipEntry ze=zis.getNextEntry();while (ze != null)
{
String fileName=ze.getName();
File newFile= new File(destDir + File.separator +fileName);//System.out.println("Unzipping to " + newFile.getAbsolutePath());//create directories for sub directories in zip
newFile(newFile.getParent()).mkdirs();
FileOutputStream fos= newFileOutputStream(newFile);intlen;while ((len = zis.read(buffer)) > 0)
{
fos.write(buffer,0, len);
}
fos.close();//close this ZipEntry
zis.closeEntry();
ze=zis.getNextEntry();
}//close last ZipEntry
zis.closeEntry();
zis.close();
fis.close();
}catch(IOException e)
{
e.printStackTrace();
}
}public static voidmain(String[] args)
{
String zipFilePath= "D:\\D\\dll.zip";
String destDir= "D:\\D\\dll_zip";
UnZip.unzip(zipFilePath, destDir);
}
}