Java版的WinRAR工具

Java版的WinRAR工具

最近做个东西,需要强大的Java版压缩组建支持,可惜没有开源的,最后实在没办法了。决定自己实现个,反正JDK中提供了最基础的API。

标题说WinRAR工具,夸大了,还没WinRAR那么强大,仅仅是一个zip工具组建,没有GUI界面,只有工具方法。

目标:
实现一个像WinRAR、WinZip一样可以同时混合压缩或者解压缩文件和文件夹的工具。目前仅支持zip文件,因为SUN Java API仅支持zip和gzip两种格式,gzip就像玩具枪,不中用,就不说了,下面是实现代码。

实现:
寥寥不到百行代码就搞定了,难点在于一个递归算法。

import java.io.*;
import java.util.Collection;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
//import org.apache.tools.zip.ZipEntry;
//import org.apache.tools.zip.ZipOutputStream;

/**
* Java版的Zip工具
*
* @author leizhimin 2008-11-27 11:16:05
*/
public class ZipUtils {
private static final int BUFF_SIZE = 1024 * 1024; //1M Byte

/**
* 批量压缩文件(夹)
*
* @param resFileList 要压缩的文件(夹)列表
* @param zipFile 生成的压缩文件
* @throws IOException 当压缩过程出错时抛出
*/
public static void zipFiles(Collection<File> resFileList, File zipFile) throws IOException {
ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile), BUFF_SIZE));
for (File resFile : resFileList) {
zipFile(resFile, zipout, "");
}
zipout.close();
}

/**
* 批量压缩文件(夹)
*
* @param resFileList 要压缩的文件(夹)列表
* @param zipFile 生成的压缩文件
* @param comment 压缩文件的注释
* @throws IOException 当压缩过程出错时抛出
*/
public static void zipFiles(Collection<File> resFileList, File zipFile, String comment) throws IOException {
ZipOutputStream zipout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile), BUFF_SIZE));
for (File resFile : resFileList) {
zipFile(resFile, zipout, "");
}
zipout.setComment(comment);
zipout.close();
}

/**
* 解压缩一个文件
*
* @param zipFile 压缩文件
* @param folderPath 解压缩的目标目录
* @throws IOException 当压缩过程出错时抛出
*/
public static void upZipFile(File zipFile, String folderPath) throws IOException {
ZipFile zf = new ZipFile(zipFile);
for (Enumeration entries = zf.entries(); entries.hasMoreElements();) {
ZipEntry entry = ((ZipEntry) entries.nextElement());
InputStream in = zf.getInputStream(entry);
OutputStream out = new FileOutputStream(folderPath + File.separator + entry.getName());
byte buffer[] = new byte[BUFF_SIZE];
int realLength;
while ((realLength = in.read(buffer)) > 0) {
out.write(buffer, 0, realLength);
}
in.close();
out.close();
}
}

private static void zipFile(File resFile, ZipOutputStream zipout, String rootpath) throws IOException {
rootpath = rootpath + (rootpath.trim().length() == 0 ? "" : File.separator) + resFile.getName();
if (resFile.isDirectory()) {
File[] fileList = resFile.listFiles();
for (File file : fileList) {
zipFile(file, zipout, rootpath);
}
} else {
byte buffer[] = new byte[BUFF_SIZE];
BufferedInputStream in = new BufferedInputStream(new FileInputStream(resFile), BUFF_SIZE);
zipout.putNextEntry(new ZipEntry(rootpath));
int realLength;
while ((realLength = in.read(buffer)) != -1) {
zipout.write(buffer, 0, realLength);
}
in.close();
zipout.flush();
zipout.closeEntry();
}
}
}

测试代码:
public class Test {
public static void main(String[] args) throws IOException {
Collection<File> resFileList = new ArrayList<File>();
resFileList.add(new File("C:\\new.gif"));
resFileList.add(new File("C:\\HelloWorld.java"));
resFileList.add(new File("C:\\crebas.sql"));
resFileList.add(new File("E:\\log.log"));
resFileList.add(new File("C:\\ooo\\upx\\"));

File zipFile = new File("C:\\txxxt.zip");

ZipUtils.zipFiles(resFileList, zipFile);
}
}

运行结果:
压缩成功!

Process finished with exit code 0

查看硬盘的上压缩文件,没错,贴个图看看:



呵呵,经过查看,没问题,就是注释乱码。

经过反复测试,发现中文支持有问题。

google了一下解决方案,用ant包中的两个类
//import org.apache.tools.zip.ZipEntry;
//import org.apache.tools.zip.ZipOutputStream;
替换Java包的对应的两个类
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
即可完美支持中文。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值