最近做个东西,需要强大的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();
}
}
}
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);
}
}
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
查看硬盘的上压缩文件,没错,贴个图看看:
![](https://i-blog.csdnimg.cn/blog_migrate/610e796524d57ed56f6005b823e797c2.png)
呵呵,经过查看,没问题,就是注释乱码。
经过反复测试,发现中文支持有问题。
google了一下解决方案,用ant包中的两个类
//import org.apache.tools.zip.ZipEntry;
//import org.apache.tools.zip.ZipOutputStream;
//import org.apache.tools.zip.ZipOutputStream;
替换Java包的对应的两个类
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipOutputStream;
即可完美支持中文。
本文转自 leizhimin 51CTO博客,原文链接:http://blog.51cto.com/lavasoft/115683,如需转载请自行联系原作者