注意:
1:使用jdk自带的zip的util的工具类实现解压zip文件,会出现中文文件名称乱码的问题,apache的zip的工具类改正了这个问题。
2:使用apache的zip的工具类,编码方式和项目的编码方式有关系,若你的项目使用的是utf-8的字符编码,则按照utf-8的方式解压缩,或你的工程的字符编码是gbk的项目编码,则按照字符集为gbk的项目编码来进行解压缩
,对于这个字符编码问题,我在写项目中亲自过.
3:若用apache的zip类库,来实现压缩或是解压缩,则还是出现文件名称乱码问题,这就需要手动设置字符编码啦。
程序代码如下:
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import org.apache.log4j.Logger;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
/**
* @author Sandy
* @version $Revision: 1.1 $ 建立日期 2011-7-2
*/
public class ZipFileUtil
{
private static final Logger logger = Logger.getLogger(ZipFileUtil.class);
private static ZipOutputStream zipOut;
private static int bufSize;
private static byte[] buf;
private static int readBytes;
private static final String _GBK = "gbk";
static
{
bufSize = 1024;
buf = new byte[bufSize];
}
/**
* 压缩文件
*
* @param zipDirectory
* @param destFileName
*/
public static void doZip(String zipDirectory, String destFileName)
{
File zipDir = new File(zipDirectory);
File destFile = new File(destFileName);
try
{
zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(destFile)));
zipOut.setEncoding(_GBK);
handleDirectory(zipDir, zipOut);
zipOut.close();
logger.info("压缩文件成功...........");
}
catch (IOException ex)
{
logger.error("压缩文件出错:" + ex.getMessage());
}
}
// call by doZip
private static void handleDirectory(File dir, ZipOutputStream zipOut) throws IOException
{
FileInputStream input;
File[] files = dir.listFiles();
if (files.length == 0)
{
zipOut.putNextEntry(new ZipEntry(dir.toString() + "/"));
zipOut.closeEntry();
} else
{
for (File sourceFile : files)
{
if (sourceFile.isDirectory())
{
handleDirectory(sourceFile, zipOut);
} else
{
input = new FileInputStream(sourceFile);
zipOut.putNextEntry(new ZipEntry(sourceFile.getName()));
while ((readBytes = input.read(buf)) > 0)
{
zipOut.write(buf, 0, readBytes);
}
zipOut.closeEntry();
}
}
}
}
public static void unZip(String unZipfileName, String targetfolder)
{
FileOutputStream output;
File file;
InputStream input;
ZipFile zipFile;
try
{
zipFile = new ZipFile(unZipfileName, _GBK);
for (Enumeration<?> entries = zipFile.getEntries(); entries.hasMoreElements();)
{
ZipEntry entry = (ZipEntry) entries.nextElement();
file = new File(targetfolder, entry.getName());
if (entry.isDirectory())
{
file.mkdirs();
} else
{
File parent = file.getParentFile();
if (!parent.exists())
{
parent.mkdirs();
}
input = zipFile.getInputStream(entry);
output = new FileOutputStream(file);
while ((readBytes = input.read(buf)) > 0)
{
output.write(buf, 0, readBytes);
}
output.close();
input.close();
}
}
zipFile.close();
logger.info("解压文件成功...........");
}
catch (IOException ex)
{
logger.error("解压文件["+unZipfileName+"]出错:" + ex.getMessage());
}
}
public static void main(String[] args) throws Exception
{
// ZipFileUtil.doZip("D:\\unzipDir", "D:\\targetZip\\organ.zip");
ZipFileUtil.unZip("D:\\ziptest\\ziptest.zip", "D:\\unzipDir");
}
}