快捷搜索:  as

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 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 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 resFileList = new ArrayList();

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;

即可完美支持中文。

您可能还会对下面的文章感兴趣: