使用JUnrar提取文件 [英] Extracting a file with JUnrar

查看:136
本文介绍了使用JUnrar提取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我早些时候问了一个有关用Java提取RAR档案的问题,有人指出我是JUNUR。官方网站已关闭,但似乎得到了广泛的使用,因为我在线上找到了很多关于它的讨论。

I asked a question earlier about extracting RAR archives in Java and someone pointed me to JUnrar. The official site is down but it seems to be quite widely used as I found a lot of discussions about it online.

有人可以告诉我如何使用JUnrar提取所有内容吗?存档中的文件?我在网上找到了一些代码段,但似乎没有用。

Could someone show me how to use JUnrar to extract all the files in an archive? I found a little snippet online but it doesn't seem to work. It shows each item in the archive to be a directory even if it is a file.

    Archive rar = new Archive(new File("C://Weather_Icons.rar"));
    FileHeader fh = rar.nextFileHeader();

    while(fh != null){
        if (fh.isDirectory()) {
             logger.severe("directory: " + fh.getFileNameString() ); 
        }

        //File out = new File(fh.getFileNameString());
        //FileOutputStream os = new FileOutputStream(out);
        //rar.extractFile(fh, os);
        //os.close();
        fh=rar.nextFileHeader();

    }

谢谢。

推荐答案

也许您还应该检查代码段代码。可以在下面找到其副本。

May be you should also check this snippet code. A copy of which can be found below.

public class MVTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String filename = "/home/rogiel/fs/home/movies/vp.mp3.part1.rar";
        File f = new File(filename);
        Archive a = null;
        try {
            a = new Archive(new FileVolumeManager(f));
        } catch (RarException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (a != null) {
            a.getMainHeader().print();
            FileHeader fh = a.nextFileHeader();
            while (fh != null) {
                try {
                    File out = new File("/home/rogiel/fs/test/"
                            + fh.getFileNameString().trim());
                    System.out.println(out.getAbsolutePath());
                    FileOutputStream os = new FileOutputStream(out);
                    a.extractFile(fh, os);
                    os.close();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (RarException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                fh = a.nextFileHeader();
            }
        }
    }
}

这篇关于使用JUnrar提取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆