0

我正在尝试将文件提取到我拥有的 .iso 文件中。没有错误,控制台打印出里面的所有文件。但是,没有文件被提取。任何帮助是极大的赞赏!

我能够找到此链接,但似乎无法将其集成到我的项目中。 链接到代码示例

       class DownloadWorker extends SwingWorker<String, Object> {
           private String flocation;
           private JLabel out;

           public DownloadWorker(String location, JLabel fout)
           {
               this.flocation = location;
               this.out = fout;
           }
       @Override
       public String doInBackground() {
           //download here
           out.setText("Beginning download. This may take a few minutes.");
           try {
                //ProgressBar/Install
                System.out.println("FILELOCATION:\n----------");
                System.out.println(flocation);
                String URL_LOCATION = "http://www.futureretrogaming.tk/gamefiles/ProfessorPhys.iso";
                String LOCAL_FILE = (flocation + "\\ProfessorPhys\\");
                File localfile = new File(LOCAL_FILE);
                if (localfile.exists()) {
                    System.out.println("Directory exists!");
                }
                else {
                    System.out.println("Directory doesn't exist! Creating...");
                    localfile.mkdir();
                    if (localfile.exists())
                        System.out.println("Directory created!");
                }
                System.out.println("LOCALFILE:\n-------");
                System.out.println(LOCAL_FILE);
                URL website = new URL(URL_LOCATION);
                ReadableByteChannel rbc = Channels.newChannel(website.openStream());
                FileOutputStream fos = new FileOutputStream(LOCAL_FILE+"\\ProfessorPhys.iso\\");
                fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
                System.out.println("--------\nDone Downloading\n---------");

                RandomAccessFile randomAccessFile = null;
        ISevenZipInArchive inArchive = null;
        try {
            randomAccessFile = new RandomAccessFile(localfile +"\\ProfessorPhys.iso\\", "r");
            inArchive = SevenZip.openInArchive(null, // autodetect archive type
                    new RandomAccessFileInStream(randomAccessFile));

            // Getting simple interface of the archive inArchive
            ISimpleInArchive simpleInArchive = inArchive.getSimpleInterface();

            System.out.println("   Hash   |    Size    | Filename");
            System.out.println("----------+------------+---------");

            for (ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) {
                final int[] hash = new int[] { 0 };
                if (!item.isFolder()) {
                    ExtractOperationResult result;

                    final long[] sizeArray = new long[1];
                    result = item.extractSlow(new ISequentialOutStream() {
                        public int write(byte[] data) throws SevenZipException {
                            hash[0] ^= Arrays.hashCode(data); // Consume data
                            sizeArray[0] += data.length;
                            return data.length; // Return amount of consumed data
                        }
                    });
                    if (result == ExtractOperationResult.OK) {
                        System.out.println(String.format("%9X | %10s | %s", // 
                                hash[0], sizeArray[0], item.getPath()));
                    } else {
                        System.err.println("Error extracting item: " + result);
                    }
                }
            }
        } catch (Exception e) {
            System.err.println("Error occurs: " + e);
            System.exit(1);
        } finally {
            if (inArchive != null) {
                try {
                    inArchive.close();
                } catch (SevenZipException e) {
                    System.err.println("Error closing archive: " + e);
                }
            }
            if (randomAccessFile != null) {
                try {
                    randomAccessFile.close();
                } catch (IOException e) {
                    System.err.println("Error closing file: " + e);
                }
            }
        }
            } catch (Exception e) {
                System.out.println(e);
            }
           return "done";
       }

       @Override
       protected void done() {
           //done
           out.setText(out.getText() + "\n Operation Done");
       }
   }

我正在尝试实现的代码:

public class ExtractorUtil {
    private static Logger logger = Logger.getLogger(ExtractorUtil.class.getCanonicalName());
    public static void extract(File file, String extractPath) throws Exception {
    ISevenZipInArchive inArchive = null;
    RandomAccessFile randomAccessFile = null;
    randomAccessFile = new RandomAccessFile(file, "r");
    inArchive = SevenZip.openInArchive(null, new RandomAccessFileInStream(randomAccessFile));
    inArchive.extract(null, false, new MyExtractCallback(inArchive, extractPath));
    if (inArchive != null) {
        inArchive.close();
    }
    if (randomAccessFile != null) {
        randomAccessFile.close();
    }
    }
    public static class MyExtractCallback implements IArchiveExtractCallback {
    private final ISevenZipInArchive inArchive;
    private final String extractPath;
    public MyExtractCallback(ISevenZipInArchive inArchive, String extractPath) {
        this.inArchive = inArchive;
        this.extractPath = extractPath;
    }
    @Override
    public ISequentialOutStream getStream(final int index, ExtractAskMode extractAskMode) throws SevenZipException {
        return new ISequentialOutStream() {
        @Override
        public int write(byte[] data) throws SevenZipException {
            String filePath = inArchive.getStringProperty(index, PropID.PATH);
            FileOutputStream fos = null;
            try {
            File dir = new File(extractPath);
            File path = new File(extractPath + filePath);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            if (!path.exists()) {
                path.createNewFile();
            }
            fos = new FileOutputStream(path, true);
            fos.write(data);
            } catch (IOException e) {
            logger.severe(e.getLocalizedMessage());
            } finally {
            try {
                if (fos != null) {
                fos.flush();
                fos.close();
                }
            } catch (IOException e) {
                logger.severe(e.getLocalizedMessage());
            }
            }
            return data.length;
        }
        };
    }
4

0 回答 0