我需要打开一个压缩文件(zml格式,我找不到有关该扩展名的信息),就像7zip在Java中所做的那样。
我有一个zml文件,如果我用7zip打开它,它会要求我输入密码,然后我输入密码就可以打开文件。
我需要在Java中实现相同的功能,请问有什么建议吗?
最好的问候。
Juan
我需要打开一个压缩文件(zml格式,我找不到有关该扩展名的信息),就像7zip在Java中所做的那样。
我有一个zml文件,如果我用7zip打开它,它会要求我输入密码,然后我输入密码就可以打开文件。
我需要在Java中实现相同的功能,请问有什么建议吗?
最好的问候。
Juan
根据@trooper的评论,我成功解压了一个受密码保护的.7z文件。请尝试以下代码。您需要使用7-Zip-JBinding设置类路径(http://sevenzipjbind.sourceforge.net/index.html)。此代码是在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();
}
}
}
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;
}