当前位置:七道奇文章资讯编程技术Java编程
日期: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编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
  • 利用Javascript实现网页水印(非图片水印)
  • Java开辟环境的搭建
  • Ubuntu java安装与配置
  • 办理Ubuntu 10.04 Firefox3.6 Java浏览器插件不工作的问
  • Ubuntu重装后Java环境的设置
  • Sun Java进入Ubuntu 10.10软件中央
  • Ubuntu 10.10配置Java开辟环境
  • 在Ubuntu 10.10中配置Java环境变量的办法
  • Ubuntu下Java环境的搭建
  • Ubuntu 10.04 下安装 Java, JRE
  • Ubuntu 10.04下的搭建SUN JAVA开辟环境
  • Ubuntu 12.04安装java7
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .