java - 7-zip-JBinding 解压文件的用法

标签 java unzip 7zip

我使用下面的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();
            }
        }
    }
}

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

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

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

最佳答案

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

Write data byte array to the stream. If data.length > 0 this function must write at least 1 byte.

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

关于java - 7-zip-JBinding 解压文件的用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33125058/

相关文章:

java - 在 Java 中解压缩包含多个文件和目录的 7zip 存档

java - 如何在某处制作 Sprite 点?

flash - Flash ActionScript 2 的压缩和解压缩工具

iphone - 在设备上解压文件

java - 使用 java jar 使用 7zip SFX 制作单个文件

java - 如何从 Java 中的 7-zip 流中提取文件而不将其存储在硬盘上?

java - Struts2:如何在 ActionSupport 中获取 ServletRequest 实例

java - @EnableGlobalMethodSecurity 在新的 spring boot 3.0 中被弃用

java - 无法解析构造函数 'BarEntry(java.lang.String, java.lang.Float)

c# - 使用 C# 解压 .gz 文件