English 中文(简体)
Issue with Zipped Streams from .Net and reading them from Java
原标题:

I m trying to zip a stream from .Net that can be read from Java code. So as input I have a byte array, which I want to compress and I m expecting to have a binary array.

I ve tested with SharpZipLib and DotNetZip to the compressed byte array, but unfortunately I always get an error when trying to uncompress it using the java.util.zip.Deflater class in Java.

Do someone have a code sample of compressing a String or a byte array with .Net and de-compressing it with the java.util.zip.Deflater class?

问题回答

You shouldn t need to touch Deflater. Deflater deals with decompressing individual entries within the zip file.

ZipInputStream is the odd class to go for. There is also ZipFile if you really need to go for random access to an actual file (for many reasons, I wouldn t recommend it).

Inflater doesn t read zip streams. It reads ZLIB (or DEFLATE) streams. The ZIP format surrounds a pure DEFLATE stream with additional metadata. Inflater doesn t handle that metadata.


If you are inflating on the Java side, you need Inflater.
On the .NET side you can use the Ionic.Zlib.ZlibStream class from DotNetZip to compress - in other words to produce something the Java Inflater can read.

I ve just tested this; this code works. The Java side decompresses what the .NET side has compressed.

.NET side:

byte[] compressed = Ionic.Zlib.ZlibStream .CompressString(originalText);
File.WriteAllBytes("ToInflate.bin", compressed);

Java side:

public void Run()
    throws java.io.FileNotFoundException,
           java.io.IOException,
           java.util.zip.DataFormatException,
           java.io.UnsupportedEncodingException,
            java.security.NoSuchAlgorithmException
{
    String filename = "ToInflate.bin";
    File file = new File(filename);
    InputStream is = new FileInputStream(file);

    // Get the size of the file
    int length = (int)file.length();

    byte[] deflated = new byte[length];

    // Read in the bytes
    int offset = 0;
    int numRead = 0;
    while (offset < deflated.length
           && (numRead=is.read(deflated, offset, deflated.length-offset)) >= 0) {
        offset += numRead;
    }


    // Decompress the bytes
    Inflater decompressor = new Inflater();
    decompressor.setInput(deflated, 0, length);
    byte[] result = new byte[100];
    int totalRead= 0; 
    while ((numRead = decompressor.inflate(result)) > 0)
        totalRead += numRead;
    decompressor.end();

    System.out.println("Inflate: total size of inflated data: " + totalRead + "
");

    result = new byte[totalRead];
    decompressor = new Inflater();        
    decompressor.setInput(deflated, 0, length);
    int resultLength = decompressor.inflate(result);
    decompressor.end();

    // Decode the bytes into a String
    String outputString = new String(result, 0, resultLength, "UTF-8");

    System.out.println("Inflate: inflated string: "  + outputString + "
");
}

(I m kinda rusty at Java so it might stand some improvement, but you get the idea)

Here is the page from Sun on ZipStreams: http://java.sun.com/developer/technicalArticles/Programming/compression/

Another library that deals with ZipStreams is POI. It is more focused on working with MS OFfic XML format docs but it might have some different insights as to how to handle the stream. http://poi.apache.org/apidocs/org/apache/poi/openxml4j/opc/internal/marshallers/ZipPartMarshaller.html





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签