【问题标题】:7-zip-JBinding usage to unzip a file7-zip-JBinding 使用解压文件
【发布时间】:2016-01-12 12:36:44
【问题描述】:

我用下面的java代码解压了一个.gz文件。 .gz 文件包含一个文件夹/文本文件。只有当文本文件中有一些数据时它才能正常工作。即使文本文件中没有数据,我也需要它工作。帮助我对以下 java 代码进行任何修改。

public static void main(String[] args) {
    String sourceZipFile = "C:/SKKIKRFULL20151014.gz";
    final String destinationDir = "C:/";
    RandomAccessFile randomAccessFile = null;
    ISevenZipInArchive inArchive = null;
    try {
        randomAccessFile = new RandomAccessFile(sourceZipFile, "r");
        inArchive = SevenZip.openInArchive(null, // autodetect archive type
                new RandomAccessFileInStream(randomAccessFile));

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

        for (final ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) {

            final int[] hash = new int[] { 0 };

            if (!item.isFolder()) {

                ExtractOperationResult result;
                result = item.extractSlow(new ISequentialOutStream() {

                    public int write(final byte[] data) throws SevenZipException {
                        try {

                            if (item.getPath().indexOf(File.separator) > 0) {

                                String path = destinationDir + File.separator
                                        + item.getPath().substring(0, item.getPath().lastIndexOf(File.separator));

                                File folderExisting = new File(path);

                                if (!folderExisting.exists()) {

                                    new File(path).mkdirs();
                                }
                            }
                            OutputStream out = new FileOutputStream(
                                    destinationDir + File.separator + item.getPath());

                            out.write(data);
                            out.close();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        hash[0] |= Arrays.hashCode(data);
                        return data.length; // Return amount of proceed data
                    }
                }); 
                if (result == ExtractOperationResult.OK) {
                    System.out.println(String.format("%9X | %s", hash[0], item.getPath()));
                } else {
                    System.err.println("Error extracting item: " + result);
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (inArchive != null) {
            try {
                inArchive.close();
            } catch (SevenZipException e) {
                System.err.println("Error closing archive: " + e);
                e.printStackTrace();
            }
        }
        if (randomAccessFile != null) {
            try {
                randomAccessFile.close();
            } catch (IOException e) {
                System.err.println("Error closing file: " + e);
                e.printStackTrace();
            }
        }
    }
}

当我调试它时。它没有进入下面的块。

 result = item.extractSlow(new ISequentialOutStream() {//write() method})

想知道如何修改代码以使其正常工作。

【问题讨论】:

  • 文件没有文本时会发生什么?我假设“无文本”意味着它的长度为 0 字节。
  • 是的。没有文本意味着它的长度为 0 字节,当没有文本时,不会发生解压缩,也不会显示错误。

标签: java unzip 7zip


【解决方案1】:

您的代码仅在其有效负载大于 0 字节时才创建文件。我相信这是因为 ISequentialOutStream write(byte[] data) - 方法仅在数组大于 0 字节时写入。来自 javadocs:

将数据字节数组写入流。如果 data.length > 0 此函数必须写入至少 1 个字节。

您应该更改 write() - 方法代码以允许创建空文件。 也许这有帮助:Stackoverflow: decompress files with .7z extension in java

【讨论】:

  • 只有当文件中没有文本时才进入write方法。我觉得它不仅仅是进入下面的块.. result = item.extractSlow(new ISequentialOutStream() { //write() method })
  • 感谢代码链接。但是链接中提供的代码只像我的代码一样工作。如果文件中没有数据,则不会解压缩。
猜你喜欢
  • 2013-08-04
  • 1970-01-01
  • 1970-01-01
  • 2021-10-08
  • 1970-01-01
  • 2012-12-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多