`
sjsky
  • 浏览: 904589 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Java解压zip和rar文件

    博客分类:
  • Java
阅读更多
       blog迁移至:http://www.micmiu.com

    本文主要讲一讲如何在java中实现对zip和rar文件的解压。
   本文的相关的源码源码下载
一、解压rar文件。
由于WinRAR 是共享软件,并不是开源的,所以解压rar文件的前提是系统已经安装了winrar,比如本人的安装路径是:
C:\\Program Files\\WinRAR\\winrar.exe
然后运用java.lang.Process 的相关知识来运行系统命令行来实现解压的。
winrar 命令行相关参数自己可以搜索下的网上资料很多
具体代码:
**
 * 解压rar文件(需要系统安装Winrar 软件)
 * @author Michael sun
 */
public class UnRarFile {
    /**
     * 解压rar文件
     * 
     * @param targetPath
     * @param absolutePath
     */
    public void unRarFile(String targetPath, String absolutePath) {

        try {

            // 系统安装winrar的路径
            String cmd = "C:\\Program Files\\WinRAR\\winrar.exe";
            String unrarCmd = cmd + " x -r -p- -o+ " + absolutePath + " "
                    + targetPath;

            Runtime rt = Runtime.getRuntime();
            Process pre = rt.exec(unrarCmd);
            InputStreamReader isr = new InputStreamReader(pre.getInputStream());
            BufferedReader bf = new BufferedReader(isr);
            String line = null;
            while ((line = bf.readLine()) != null) {
                line = line.trim();
                if ("".equals(line)) {
                    continue;
                }
                System.out.println(line);
            }

            bf.close();
        } catch (Exception e) {
            System.out.println("解压发生异常");
        }
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        String targetPath = "D:\\test\\unrar\\";
        String rarFilePath = "D:\\test\\test.rar";
        UnRarFile unrar = new UnRarFile();
        unrar.unRarFile(targetPath, rarFilePath);
    }

}

二、解压zip文件
由于zip是免费的,所以在jdk里提供了相应的类对zip文件的实现:
java.util.zip.*,详细情况可以参考java API
/**
 * 解压zip文件
 * @author Michael sun
 */
public class UnzipFile {

    /**
     * 解压zip文件
     * 
     * @param targetPath
     * @param zipFilePath
     */
    public void unzipFile(String targetPath, String zipFilePath) {

        try {
            File zipFile = new File(zipFilePath);
            InputStream is = new FileInputStream(zipFile);
            ZipInputStream zis = new ZipInputStream(is);
            ZipEntry entry = null;
            System.out.println("开始解压:" + zipFile.getName() + "...");
            while ((entry = zis.getNextEntry()) != null) {
                String zipPath = entry.getName();
                try {

                    if (entry.isDirectory()) {
                        File zipFolder = new File(targetPath + File.separator
                                + zipPath);
                        if (!zipFolder.exists()) {
                            zipFolder.mkdirs();
                        }
                    } else {
                        File file = new File(targetPath + File.separator
                                + zipPath);
                        if (!file.exists()) {
                            File pathDir = file.getParentFile();
                            pathDir.mkdirs();
                            file.createNewFile();
                        }

                        FileOutputStream fos = new FileOutputStream(file);
                        int bread;
                        while ((bread = zis.read()) != -1) {
                            fos.write(bread);
                        }
                        fos.close();

                    }
                    System.out.println("成功解压:" + zipPath);

                } catch (Exception e) {
                    System.out.println("解压" + zipPath + "失败");
                    continue;
                }
            }
            zis.close();
            is.close();
            System.out.println("解压结束");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        String targetPath = "D:\\test\\unzip";
        String zipFile = "D:\\test\\test.zip";
        UnzipFile unzip = new UnzipFile();
        unzip.unzipFile(targetPath, zipFile);
    }

}

分享到:
评论
12 楼 cxl2086 2011-07-06  
jdk对zip的解压,中文会有乱码,用ant.jar 试过就没有乱码
11 楼 enefry 2011-03-21  
unrar解压好像是免费的吧.
10 楼 mqlfly2008 2010-08-29  
zip的解压算法是正确的,可以用到程序中!但如果在服务器端解压后传到view层时会出现乱码问题,以前是通过ant来解决的!希望能够看到其他的解决方式
9 楼 aa00aa00 2010-08-20  
windchill_java 写道
如此简单的东西,难道你解压或压缩还得指定WinRAR 的安装路径吗,搞笑很


是啊,你有什么好的办法,可以拿出来啊,呵呵
8 楼 sjsky 2010-03-31  
windchill_java 写道
如此简单的东西,难道你解压或压缩还得指定WinRAR 的安装路径吗,搞笑很

希望这位仁兄 给点解决方案能实现对rar文件的解压
7 楼 windchill_java 2010-03-31  
如此简单的东西,难道你解压或压缩还得指定WinRAR 的安装路径吗,搞笑很
6 楼 ray_linn 2010-03-31  
马上就见新手帖
5 楼 sjsky 2010-03-31  
mathfox 写道
真是有创意,这都能想出来

这是由于客户的特殊需要,需要对上传的压缩文件(可能rar 也可能是zip),解压后再对文件做相关处理,
所以想到了这样的办法  现在共享出来,希望对大家有所帮助
4 楼 sjsky 2010-03-31  
fjlyxx 写道
一个微软的算法 一个标准通用的算法 怎么会一样呢.

是不是我写的有歧义? 我没有说这两个算法是一样的,我是分开单独讲这两种文件的解压
3 楼 mathfox 2010-03-31  
真是有创意,这都能想出来
2 楼 mgcnrx11 2010-03-31  
不错,zip解压正好需要用到
1 楼 fjlyxx 2010-03-31  
一个微软的算法 一个标准通用的算法 怎么会一样呢.

相关推荐

Global site tag (gtag.js) - Google Analytics