【问题标题】:Decompressing a file like 7zip with Java用Java解压7zip之类的文件
【发布时间】:2016-06-29 17:02:44
【问题描述】:
我需要打开一个压缩文件(zml,我找不到有关该扩展名的信息),就像 7zip 使用 java 一样。
我有一个zml文件,如果我用7zip打开它会要求我输入密码,然后我输入密码就可以打开文件了。
我需要对 java 做同样的事情,有人可以给我一个建议吗?
最好的问候。
胡安
【问题讨论】:
标签:
java
7zip
compression
【解决方案1】:
根据@trooper 的评论,我能够提取一个受密码保护的 .7z 文件。试试下面的代码。您需要使用 7-Zip-JBinding (http://sevenzipjbind.sourceforge.net/index.html) 设置您的类路径。此代码是代码 sn-ps 的修改版本,位于 http://sevenzipjbind.sourceforge.net/extraction_snippets.html
import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
import java.util.Arrays;
import net.sf.sevenzipjbinding.ExtractOperationResult;
import net.sf.sevenzipjbinding.IInArchive;
import net.sf.sevenzipjbinding.ISequentialOutStream;
import net.sf.sevenzipjbinding.SevenZip;
import net.sf.sevenzipjbinding.SevenZipException;
import net.sf.sevenzipjbinding.SevenZipNativeInitializationException;
import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream;
import net.sf.sevenzipjbinding.simple.ISimpleInArchive;
import net.sf.sevenzipjbinding.simple.ISimpleInArchiveItem;
public class Extract {
public static void main(String[] args) throws SevenZipException, FileNotFoundException {
try {
SevenZip.initSevenZipFromPlatformJAR();
System.out.println("7-Zip-JBinding library was initialized");
RandomAccessFile randomAccessFile = new RandomAccessFile("YOUR FILE NAME", "r");
IInArchive inArchive = SevenZip.openInArchive(null, // Choose format
// automatically
new RandomAccessFileInStream(randomAccessFile));
System.out.println(inArchive.getNumberOfItems());
// 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
for (byte b : data) {
System.out.println((char) b);
}
sizeArray[0] += data.length;
return data.length; // Return amount of consumed
// data
}
}, "YOUR PASSWORD HERE");
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 (SevenZipNativeInitializationException e) {
e.printStackTrace();
}
}
}
【解决方案3】:
下面的函数会解压你的 7zip 文件并返回解压后的文件列表。
public static List<String> unSevenZipFile(String zipFilePath, String unzipDestination, String attachmentPassword) {
List<String> extractedFilesList=new ArrayList<String>();
// Get 7zip file
try (SevenZFile sevenZFile = new SevenZFile(new File(zipFilePath),attachmentPassword.toCharArray())) {
SevenZArchiveEntry entry;
while ((entry = sevenZFile.getNextEntry()) != null) {
File file = new File(unzipDestination+ entry.getName());
System.out.println("Un seven zipping - " + file);
byte[] content = new byte[(int) entry.getSize()];
sevenZFile.read(content);
Files.write(file.toPath(), content);
extractedFilesList.add(unzipDestination+ entry.getName());
}
} catch (IOException e) {
e.printStackTrace();
}
return extractedFilesList;
}