Using 7zip SDK to compress strings (not files)

Help
Sven Tiffe
2013-09-13
2013-09-14
  • Sven Tiffe
    Sven Tiffe
    2013-09-13

    Is it possible and feasible to use the 7 zip SDK to compress medium to large size strings (1k to 80k of size)? For example, retrieving a string for a service and compressing in Java like this:

    // setup input and output
    String str = "verylargestringfromrandomsource";
    InputStream inStream = new ByteArrayInputStream(str.getBytes());
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();

    // set up encoder
    SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder();
    encoder.SetEndMarkerMode(true);
    encoder.WriteCoderProperties(outStream);

    // encode and get bytes
    encoder.Code(inStream, outStream, -1, -1, null);
    byte[] retval= outStream.toByteArray();

    Is this feasible? In the SDK example for single file encoding, the actual coded data is prefixed with eight bytes of file size information, do I need this for compressing a string that is not stored as file, when I use the end marker mode?

     
  • Igor Pavlov
    Igor Pavlov
    2013-09-14

    Probably you can do it.
    The size field is not required, if you use EndMarker.
    But the decoder will not know the decompressed size in that case. So you must use some dynamic allocation for decoding output.