I need to open a compresed file (zml, i don't couldn't find information about that extension) like 7zip does it with java.
I have a zml file, if I open it with 7zip it asks me for the password, then I put the password and can open the file.
I need to do the same with java, could anyone give me an advice for this?
Best regards.
Juan
The 7-Zip-JBinding consists of two parts: the java part and the native part. The java part introduces a cross-platform java interface of the 7-Zip library.
To unzip a zip file, we need to read the zip file with ZipInputStream and then read all the ZipEntry one by one. Then use FileOutputStream to write them to file system. We also need to create the output directory if it doesn't exists and any nested directories present in the zip file.
Creating a zip archive for a single file is very easy, we need to create a ZipOutputStream object from the FileOutputStream object of destination file. Then we add a new ZipEntry to the ZipOutputStream and use FileInputStream to read the source file to ZipOutputStream object.
Right-click the file, point to the “7-Zip” submenu, and then click the “Open Archive” command. This opens 7-zip and displays the contents of the archive. From here, you can extract the contents to another location on your hard drive using the “Extract” button at the top.
Based on @trooper comment, I was able to extract a .7z file that was password protected. Try the following code. You will need to setup your classpath with 7-Zip-JBinding (http://sevenzipjbind.sourceforge.net/index.html). This code is a modified version of code snippets found at 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();
}
}
}
If you look for a pure java solution, you can use Apache Commons Compress, which also supports reading encrypted files.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With