Permalink
Browse files

flag for compatibility with lzma(1) tool

The default is now to be compatible with the lzma(1) command-line
tool. Note that this is the single-file processor (like gzip) not the
multi-file archive tool (like zip). To retain compatibility with
previous version, or to save a few bytes, set
LzmaOutputStream.LZMA_HEADER

darcs-hash:20090312175815-e5a07-29a8376a2f55a222999020b834e2bfd31410e94b.gz
  • Loading branch information...
1 parent e40f6b5 commit 2b488bae7540dcb81e0e6a6007aa41fb58b10859 @league committed Mar 12, 2009
@@ -57,10 +57,26 @@
public void run( )
{
try {
- // int n = in.read( props, 0, propSize );
- dec.SetDecoderProperties( props );
+ long outSize = 0;
+ if( LzmaOutputStream.LZMA_HEADER ) {
+ int n = in.read( props, 0, propSize );
+ if( n != propSize )
+ throw new IOException("input .lzma file is too short");
+ dec.SetDecoderProperties( props );
+ for (int i = 0; i < 8; i++)
+ {
+ int v = in.read();
+ if (v < 0)
+ throw new IOException("Can't read stream size");
+ outSize |= ((long)v) << (8 * i);
+ }
+ }
+ else {
+ outSize = -1;
+ dec.SetDecoderProperties( props );
+ }
if(DEBUG) dbg.printf("%s begins%n", this);
- dec.Code( in, out, -1 );
+ dec.Code( in, out, outSize );
if(DEBUG) dbg.printf("%s ends%n", this);
in.close( ); //?
out.close( );
@@ -58,8 +58,13 @@ public void run( )
{
try {
enc.SetEndMarkerMode( true );
- // enc.WriteCoderProperties( out );
- // 5d 00 00 10 00
+ if( LzmaOutputStream.LZMA_HEADER ) {
+ enc.WriteCoderProperties( out );
+ // 5d 00 00 10 00
+ long fileSize = -1;
+ for (int i = 0; i < 8; i++)
+ out.write((int)(fileSize >>> (8 * i)) & 0xFF);
+ }
if(DEBUG) dbg.printf("%s begins%n", this);
enc.Code( in, out, -1, -1, null );
if(DEBUG) dbg.printf("%s ends%n", this);
@@ -0,0 +1,38 @@
+// LzmaCompat.java -- test program for compatibility with lzma(1)
+// Copyright (c)2009 Christopher League <league@contrapunctus.net>
+
+// This is free software, but it comes with ABSOLUTELY NO WARRANTY.
+// GNU Lesser General Public License 2.1 or Common Public License 1.0
+
+package net.contrapunctus.lzma;
+
+import java.io.*;
+
+// Expected performance:
+//
+// % java -cp lzmajio.jar net.contrapunctus.lzma.LzmaCompat file.7za t
+// LZMA_HEADER = true
+// writing file.7za
+// % lzma -dc file.7za
+// Hello, world!
+//
+// % java -cp lzmajio.jar net.contrapunctus.lzma.LzmaCompat file.7za
+// LZMA_HEADER = false
+// writing file.7za
+// % lzma -dc file.7za
+// ===ERROR===
+
+public class LzmaCompat
+{
+ public static void main( String[] args ) throws IOException
+ {
+ LzmaOutputStream.LZMA_HEADER = args.length > 1;
+ System.out.println("LZMA_HEADER = " + LzmaOutputStream.LZMA_HEADER);
+ System.out.println("writing " + args[0]);
+ FileOutputStream fos = new FileOutputStream(args[0]);
+ LzmaOutputStream lo = new LzmaOutputStream( fos );
+ PrintStream ps = new PrintStream( lo );
+ ps.println("Hello, world!");
+ ps.close(); // is that enough?
+ }
+}
@@ -20,6 +20,11 @@
{
protected EncoderThread eth;
+ public static boolean LZMA_HEADER = true;
+ /* true for compatibility with lzma(1) command-line tool, false
+ * for compatibility with previous versions of LZMA streams.
+ */
+
private static final PrintStream dbg = System.err;
private static final boolean DEBUG;
@@ -10,7 +10,7 @@
public class RoundTrip
{
- public static void main( String[] args ) throws IOException
+ public static void doit( ) throws IOException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
LzmaOutputStream lo = new LzmaOutputStream( baos );
@@ -34,4 +34,12 @@ public static void main( String[] args ) throws IOException
System.out.println( k );
assert s.equals( k );
}
+
+ public static void main( String[] args ) throws IOException
+ {
+ LzmaOutputStream.LZMA_HEADER = true;
+ doit();
+ LzmaOutputStream.LZMA_HEADER = false;
+ doit();
+ }
}

0 comments on commit 2b488ba

Please sign in to comment.