Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementation of 7Zip in JAVA

Tags:

java

aes

7zip

I have downloaded the LZMA SDK from the 7zip website but to my disappointment it does only support compression and decompression and does not support AES crypto. Does anyone know if there is any implementation of 7zip with AES crypto completely in JAVA?. Thanks.

Regards, Kal.

like image 763
user419268 Avatar asked Aug 13 '10 07:08

user419268


People also ask

Does 7zip use Java?

The native part of 7-Zip-JBinding communicates with the java part through the java jni interface and uses the corresponding native 7-Zip interface to perform operations.

What is a 7z file?

What is a 7z file extension? The 7z file extension is a compressed archive format created with 7-zip open-source software. It's similar to a ZIP file but uses a different lossless compression method called LZMA to reduce file size while preserving quality.


2 Answers

From apache common-compress documentation:

Note that Commons Compress currently only supports a subset of compression and encryption algorithms used for 7z archives. For writing only uncompressed entries, LZMA, LZMA2, BZIP2 and Deflate are supported - in addition to those reading supports AES-256/SHA-256 and DEFLATE64.

If you use common-compress you probably will not have issues with portability of your code as you don't have to embed any native libraries.

Below code shows how to iterate over files within 7zip archive and print its contents to stdout. You can adapt it for AES requirements :

public static void showContent(String archiveFilename) throws IOException {

    if (archiveFilename == null) {
        return;
    }

    try (SevenZFile sevenZFile = new SevenZFile(new File(archiveFilename))) {
        SevenZArchiveEntry entry = sevenZFile.getNextEntry();
        while (entry != null) {
                final byte[] contents = new byte[(int) entry.getSize()];
                int off = 0;
                while ((off < contents.length)) {
                    final int bytesRead = sevenZFile.read(contents, off, contents.length - off);
                    off += bytesRead;
                }
                System.out.println(new String(contents, "UTF-8"));              
            entry = sevenZFile.getNextEntry();
        }
    }
}

Imports used:

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
import org.apache.commons.compress.archivers.sevenz.SevenZFile;

Maven Dependencies Used:

    <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-compress -->
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-compress</artifactId>
        <version>1.18</version>
    </dependency>
    <dependency>
        <groupId>org.tukaani</groupId>
        <artifactId>xz</artifactId>
        <version>1.6</version>
    </dependency>

Please note: org.tukaani:xz is required for 7zip only. common-compress dependency doesn't need it for other supported compression formats.

like image 115
aprodan Avatar answered Oct 01 '22 01:10

aprodan


According 7Zip team:

LZMA SDK doesn't support crypto methods. Use 7-Zip source code instead.

The source code is available in assembler, C and C++, you can call them from Java.

like image 21
greuze Avatar answered Sep 30 '22 23:09

greuze