本文来源:
http://code.google.com/p/javawing/source/browse/trunk/Utils/src/org/javawing/util/ZipUtil.java?r=63
/**
* $Log: ZipCompress.java,v $
*/
package org.javawing.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.CRC32;
import java.util.zip.CheckedOutputStream;
import java.util.zip.Deflater;
import java.util.zip.ZipException;
import org.apache.log4j.Logger;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import org.apache.tools.zip.ZipOutputStream;
/**
*
@brief
* <p><b>Zip file tool class</b></p>
*
* compress and decompress file
*
* <p><center>COPYRIGHT (C) 2009,RiseTek Systems Inc.All Rights Reserved.</center></p>
* @author TangJian
* @version 1.0
* @see
* @since 2009-12-10
*/
public class ZipUtil {
private static boolean isCreateSrcDir = false;//是否创建源目录
private static Logger log = Logger.getLogger(ZipUtil.class.getName());
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
String src = "e:/temp/src";//指定压缩源,可以是目录或文件
String decompressDir = "e:/temp/decompress";//解压路径
String archive = "e:/temp/test.wid";//压缩包路径
String comment = "Java Zip semiwolf@gmail.com";//压缩包注释
//----压缩文件或目录
writeByApacheZipOutputStream(src, archive, comment);
//----使用apace ZipFile读取压缩文件
readByApacheZipFile(archive, decompressDir);
// File file = new File(archive);
// readByApacheZipFile(file, decompressDir);
}
public static void writeByApacheZipOutputStream(String src, String archive,
String comment) throws FileNotFoundException, IOException {
//----压缩文件:
FileOutputStream f = new FileOutputStream(archive);
//使用指定校验和创建输出流
CheckedOutputStream csum = new CheckedOutputStream(f, new CRC32());
ZipOutputStream zos = new ZipOutputStream(csum);
//支持中文
zos.setEncoding("GBK");
BufferedOutputStream out = new BufferedOutputStream(zos);
//设置压缩包注释
zos.setComment(comment);
//启用压缩
zos.setMethod(ZipOutputStream.DEFLATED);
//压缩级别为最强压缩,但时间要花得多一点
zos.setLevel(Deflater.BEST_COMPRESSION);
File srcFile = new File(src);
if (!srcFile.exists() || (srcFile.isDirectory() && srcFile.list().length == 0)) {
throw new FileNotFoundException(
"File must exist and ZIP file must have at least one entry.");
}
//获取压缩源所在父目录
src = src.replaceAll("\\\\", "/");
String prefixDir = null;
if (srcFile.isFile()) {
prefixDir = src.substring(0, src.lastIndexOf("/") + 1);
} else {
prefixDir = (src.replaceAll("/$", "") + "/");
}
//如果不是根目录
if (prefixDir.indexOf("/") != (prefixDir.length() - 1) && isCreateSrcDir) {
prefixDir = prefixDir.replaceAll("[^/]+/$", "");
}
//开始压缩
writeRecursive(zos, out, srcFile, prefixDir);
out.close();
// 注:校验和要在流关闭后才准备,一定要放在流被关闭后使用
log.info("Checksum: " + csum.getChecksum().getValue());
}
public static void readByApacheZipFile(File file, String decompressDir)
throws IOException, FileNotFoundException, ZipException {
ZipFile zf = new ZipFile(file, "GBK");// 支持中文
readByApacheZipFile(decompressDir, zf);
}
/**
* 使用 org.apache.tools.zip.ZipFile 解压文件,它与 java 类库中的
* java.util.zip.ZipFile 使用方式是一致的,只不过多了设置编码方式的
* 接口。
*
* 注,apache 没有提供 ZipInputStream 类,所以只能使用它提供的ZipFile
* 来读取压缩文件。
* @param archive 压缩包路径
* @param decompressDir 解压路径
* @throws IOException
* @throws FileNotFoundException
* @throws ZipException
*/
public static void readByApacheZipFile(String archive, String decompressDir)
throws IOException, FileNotFoundException, ZipException {
ZipFile zf = new ZipFile(archive, "GBK");//支持中文
readByApacheZipFile(decompressDir, zf);
}
/**
* @brief
* main extract method
*
* @param decompressDir
* @param zf
* @throws FileNotFoundException
* @throws IOException
* @throws ZipException
*/
private static void readByApacheZipFile(String decompressDir, ZipFile zf)
throws FileNotFoundException, IOException, ZipException {
BufferedInputStream bi;
Enumeration<?> e = zf.getEntries();
while (e.hasMoreElements()) {
ZipEntry ze2 = (ZipEntry) e.nextElement();
String entryName = ze2.getName();
String path = decompressDir + "/" + entryName;
if (ze2.isDirectory()) {
log.info("正在创建解压目录 - " + entryName);
File decompressDirFile = new File(path);
if (!decompressDirFile.exists()) {
decompressDirFile.mkdirs();
}
} else {
log.info("正在创建解压文件 - " + entryName);
String fileDir = path.substring(0, path.lastIndexOf("/"));
File fileDirFile = new File(fileDir);
if (!fileDirFile.exists()) {
fileDirFile.mkdirs();
}
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(decompressDir + "/" + entryName));
bi = new BufferedInputStream(zf.getInputStream(ze2));
byte[] readContent = new byte[1024];
int readCount = bi.read(readContent);
while (readCount != -1) {
//解压对文件二进制加密
bos.write(readContent, 0, readCount);
readCount = bi.read(readContent);
}
bos.close();
}
}
zf.close();
}
/**
* 递归压缩
*
* 使用 org.apache.tools.zip.ZipOutputStream 类进行压缩,它的好处就是支持中文路径,
* 而Java类库中的 java.util.zip.ZipOutputStream 压缩中文文件名时压缩包会出现乱码。
* 使用 apache 中的这个类与 java 类库中的用法是一新的,只是能设置编码方式了。
*
* @param zos
* @param bo
* @param srcFile
* @param prefixDir
* @throws IOException
* @throws FileNotFoundException
*/
private static void writeRecursive(ZipOutputStream zos, BufferedOutputStream bo,
File srcFile, String prefixDir) throws IOException, FileNotFoundException {
ZipEntry zipEntry;
String filePath = srcFile.getAbsolutePath().replaceAll("\\\\", "/").replaceAll(
"//", "/");
if (srcFile.isDirectory()) {
filePath = filePath.replaceAll("/$", "") + "/";
}
String entryName = filePath.replace(prefixDir, "").replaceAll("/$", "");
if (srcFile.isDirectory()) {
if (!"".equals(entryName)) {
log.info("正在创建目录 - " + srcFile.getAbsolutePath()
+ " entryName=" + entryName);
//如果是目录,则需要在写目录后面加上 /
zipEntry = new ZipEntry(entryName + "/");
zos.putNextEntry(zipEntry);
}
File srcFiles[] = srcFile.listFiles();
for (int i = 0; i < srcFiles.length; i++) {
writeRecursive(zos, bo, srcFiles[i], prefixDir);
}
} else {
log.info("正在写文件 - " + srcFile.getAbsolutePath() + " entryName="
+ entryName);
BufferedInputStream bi = new BufferedInputStream(new FileInputStream(srcFile));
//开始写入新的ZIP文件条目并将流定位到条目数据的开始处
zipEntry = new ZipEntry(entryName);
zos.putNextEntry(zipEntry);
byte[] buffer = new byte[1024];
int readCount = bi.read(buffer);
while (readCount != -1) {
bo.write(buffer, 0, readCount);
readCount = bi.read(buffer);
}
//注,在使用缓冲流写压缩文件时,一个条件完后一定要刷新一把,不
//然可能有的内容就会存入到后面条目中去了
bo.flush();
//文件读完后关闭
bi.close();
}
}
}
分享到:
相关推荐
网上很多描述java解压中文乱码的问题,很多描述不全.由于工作需要整理出一个完整版.简单实用.下载后请从ZipUtil.java的main方法开始,一目了然. public static void ...将E:\\aaaa\\中文.zip解压到E:\\aaaa\\中文目录下
java压缩文件成zip实现无乱码 内置类与jar包
使用java压缩也解压zip文件方法,解决中文乱码问题。使用java自带的压缩解压算法,会出现中文乱码问题。使用apache io的zip包,有效解决该问题。Ant的压缩解压,也是使用该类。
ZipEntry zipEntry = new ZipEntry(relativePath); zos.putNextEntry(zipEntry); zos.closeEntry(); } /** * * 功能描述:解压缩文件 * 创建者:XXX * 创建日期: 2015年5月7日 - 下午1:39:32...
项目中碰到问题.jdk zipEntry 压缩中文文件名乱码 上网查了下,有两种方法,一种修改jdk ZipInputStream及ZipOutputStream 的源文件,比较麻烦,不建议此项. 第二种 就是拿来主义,因为 开源项目 Ant 里已经有...
从其他网站上下载的库,修改了字符串编码转换的方法,解决中文乱码问题,使用示例可见https://blog.csdn.net/qq_28742901/article/details/116156457
应用SharpCompress0.22开源包进行文件的解压缩(zip,rar文件),已处理中文乱码问题,例子很基础.源码环境 VS2017 15.3 .
修改两处源码,解决压缩后中文名乱码问题,经过项目测试,请放心使用
CompresszZipFile 中带是 zip解压 ZipFileUtil 是文件的压缩 和rar的解压
解压过程中会出现乱码,当然压缩也是一样,使用这里面的zip 可以解决了,只是将原码改动了下。做人要老实,我这是转载被人的!
解决android 自带zip解压缩中文乱码的问题,导入zip.jar即可使用(与系统提供的用法相同),大小仅24k
在使用Java对ZIP压缩文件进行解压的方式中有两种,一种是使用apache提供的ant.jar工具包,但是如果ZIP文件中含有中文名称的文件,在解压后,文件名将出现乱码,另一种是使用Java自身JDK中java.util.zip包下工具类,...
在ubuntu10.04下用gnome自带的归档管理器解压windows下压缩的zip文件,中文会出现乱码。将压缩包中以7z开头的文件全部复制到/usr/lib/p7zip/目录下,替换掉原有的文件。这样乱码就解决了。
内核使用7Z软件进行解压 ,基本能适用所有的压缩格式, 处理解压文件时中文乱码问题, 复制代码即可使用
自己封装的文件ZIP压缩和解压接口,调用简单方便
用java.util.zipoutputstream压缩会出现中文的文件名乱码的情况,且无法设置字符集,这个版本用org.apache.tools.zip.ZipOutputStream压缩,可以自定义字符集,解决中文的文件名乱码问题。
在android开发中,有时候需要将文件打包成zip或将zip解压,如果直接用java.util.zip包下面的类会出现中文乱码,如果直接用org.apache.tools.zip(ant包中的压缩功能)会影响项目的编译时间; 故将ant包中文件瘦身,既...
python3 解决zip解压中文乱码问题,亲测可用, 直接替换zipfile.py文件,记得提前备份
zip解压缩乱码的解决方案,解决常见字符串乱码的问题,特点:易上手,可二次开发和封装.
本文介绍的是Ubuntu解压zip文件乱码的解决方法,共有2种方式解决问题,下面话不多说,来一起看看吧 一、通过unzip行命令解压,指定字符集 unzip -O CP936 xxx.zip (用GBK, GB18030也可以) 有趣的是unzip的manual中...