How to create zip with lzma compression
Asked Answered
M

3

9

I know how to create zip archive:

import java.io.*;
import java.util.zip.*;
public class ZipCreateExample{
    public static void main(String[] args)  throws Exception  
        // input file 
        FileInputStream in = new FileInputStream("F:/sometxt.txt");

        // out put file 
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream("F:/tmp.zip"));

        // name the file inside the zip  file 
        out.putNextEntry(new ZipEntry("zippedjava.txt")); 

        // buffer size
        byte[] b = new byte[1024];
        int count;

        while ((count = in.read(b)) > 0) {
            System.out.println();
            out.write(b, 0, count);
        }
        out.close();
        in.close();
    }
}

But I have no idea how to use lzma compression.

I found this project: https://github.com/jponge/lzma-java which creating compressed file but I don't know how I should combine it with my existing solution.

Marla answered 8/7, 2013 at 9:40 Comment(1)
Neither Java's Zip util nor Commons-Compress support LZMA compression for each ZipEntry. It would probably take a day or two to extended Commons-Compress to support it by using the LZMA code above and overriding the checks for STORAGE | DEFLATE. In fact, it'd be nice if Commons-Compress could use a more extensible approach where the ZipArchiveEntries were extended with the required compression method, such as ZipArchiveEntryLZMA. As it is, there's too many checks in ZipArchiveOutputStream to do this quickly.Fustic
S
3

The latest version of Apache Commons Compress (1.6 released on 23-Oct-2013) supports LZMA compression.

Have a look at http://commons.apache.org/proper/commons-compress/examples.html, specially the one regarding .7z compressing/uncompressing.

Say for example you want to store an html page from an HTTP Response and you want to compress it:

SevenZOutputFile sevenZOutput = new SevenZOutputFile(new File("outFile.7z"));

File entryFile = new File(System.getProperty("java.io.tmpdir") + File.separator + "web.html");
SevenZArchiveEntry entry = sevenZOutput.createArchiveEntry(entryFile, "web.html");

sevenZOutput.putArchiveEntry(entry);
sevenZOutput.write(rawHtml.getBytes());
sevenZOutput.closeArchiveEntry();
sevenZOutput.close();
Seraphina answered 8/11, 2013 at 10:8 Comment(0)
P
0

There is an example in the website you mentioned:

Adapted to your needs:

final File sourceFile = new File("F:/sometxt.txt");
final File compressed = File.createTempFile("lzma-java", "compressed");

final LzmaOutputStream compressedOut = new LzmaOutputStream.Builder(
        new BufferedOutputStream(new FileOutputStream(compressed)))
        .useMaximalDictionarySize()
        .useEndMarkerMode(true)
        .useBT4MatchFinder()
        .build();

final InputStream sourceIn = new BufferedInputStream(new FileInputStream(sourceFile));

IOUtils.copy(sourceIn, compressedOut);
sourceIn.close();
compressedOut.close();

(I don't know if it works, it is just the usage of the library and your code snippet)

Priestley answered 8/7, 2013 at 10:51 Comment(3)
as I said in my question this just create compressed file not compressed archiveMarla
Isn't it the same?...The output is just a compressed stream of bytes of the input(s), which is then written to a file.Priestley
nope it isnt because archive may contains a lot of compressed fileMarla
M
0

zip4jvm supports LZMA compression for zip archive.

ZipEntrySettings entrySettings = ZipEntrySettings.builder()
                                                 .compression(Compression.LZMA, CompressionLevel.NORMAL)
                                                 .lzmaEosMarker(true).build();
ZipSettings settings = ZipSettings.builder().entrySettingsProvider(fileName -> entrySettings).build();

Path file = Paths.get("F:/sometxt.txt");
Path zip = Paths.get("F:/tmp.zip");

ZipIt.zip(zip).settings(settings).add(file);
Monegasque answered 13/2, 2020 at 23:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.