gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:17:44 30 4
gpt4 key购买 nike

我使用下面的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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com