Extracting a file with JUnrar

前端 未结 1 682
情书的邮戳
情书的邮戳 2021-01-05 09:05

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

相关标签:
1条回答
  • 2021-01-05 09:32

    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();
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题