日期:2011-01-26 02:54:00 来源:本站整理
Java版的WinRAR工具[Java编程]
本文“Java版的WinRAR工具[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
近来做个东西,需求强盛的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(); } } } |
以上是“Java版的WinRAR工具[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |
评论内容只代表网友观点,与本站立场无关!
评论摘要(共 0 条,得分 0 分,平均 0 分)
查看完整评论