11 Replies - 8223 Views - Last Post: 18 August 2011 - 08:22 PM Rate Topic: -----

#1 Skick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-April 10

Compression and decompression between Java and C#

Posted 16 August 2011 - 05:09 PM

For the past week or so I've been trying to write compression and decompression code that is compatible between both Java and C# (for example, compress a string in the Java app and decompress it in the C# app). I have tried GZipStream (C#) / GZIPOutputStream (Java), Zip, 7zip, etc, yet nothing seems to be compatible between both languages.

Does anyone have any code/can anyone help out?

Thanks for your time
Is This A Good Question/Topic? 0
  • +

Replies To: Compression and decompression between Java and C#

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Compression and decompression between Java and C#

Posted 16 August 2011 - 05:18 PM

Java ZipInputStream and ZipOutputStream should read/write "standard" Zip files.
Was This Post Helpful? 0
  • +
  • -

#3 Skick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-April 10

Re: Compression and decompression between Java and C#

Posted 16 August 2011 - 05:24 PM

My latest code consists of using the ZipOutputStream/ZipInputStream in Java, and the DotNetZipLib library (which I downloaded from http://dotnetzip.codeplex.com/).

I have tried compressing a string in Java and then decompressing the resulting bytes in C#, which actually provides the correct string when decompressed, however there are excess bytes after the string.

When I tried to compress the string in C# and then decompress it in Java, however, the data was not readable.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Compression and decompression between Java and C#

Posted 16 August 2011 - 05:30 PM

Did you compressed the String object or the actual bytes in the String object ?

*Edited: stupid question you cannot write Object to a ZipOutputStream

This post has been edited by pbl: 16 August 2011 - 05:36 PM

Was This Post Helpful? 0
  • +
  • -

#5 Skick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-April 10

Re: Compression and decompression between Java and C#

Posted 16 August 2011 - 05:44 PM

I am compressing the byte array of the string. I'm now testing DeflateStream, I'll keep you posted on how it goes.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Compression and decompression between Java and C#

Posted 16 August 2011 - 05:58 PM

Yes, first test try to read it back from Java
Don't use the Deflater, use ZipInputStream
Was This Post Helpful? 0
  • +
  • -

#7 Skick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-April 10

Re: Compression and decompression between Java and C#

Posted 16 August 2011 - 07:05 PM

I already tried using Zip but the output that the two languages produced was not compatible with the other. I've just successfully used DeflateStream (C#) to compress/decompress data which is compatible with DeflaterOutputStream (compress) and InflaterInputStream (decompress) in Java. Here is the code, in case anyone ever needs to do the same:

C# Compression:
private byte[] DeflaterCompress(byte[] toCompress)
{
	using (MemoryStream compressedStream = new MemoryStream())
	{
		compressedStream.Position = 0;
		using (DeflateStream deflater = new DeflateStream(compressedStream, CompressionMode.Compress))
		{
			deflater.Write(toCompress, 0, toCompress.Length);
		}
		
		return compressedStream.ToArray();
	}
}


C# Decompression:
private byte[] DeflaterDecompress(byte[] toDecompress)
{
	using (MemoryStream decompressedStream = new MemoryStream())
	{
		using (MemoryStream compressedStream = new MemoryStream(toDecompress))
		{
			using (DeflateStream deflater = new DeflateStream(compressedStream, CompressionMode.Decompress))
			{
				int c = 0;
				while ((c = deflater.ReadByte()) != -1)
				{
					decompressedStream.WriteByte((byte)c);
				}
			}
		}
		return decompressedStream.ToArray();
	}
}


Java Compression:
private byte[] deflaterCompress(byte[] toCompress)
{
	try
	{
	    ByteArrayOutputStream compressedStream = new ByteArrayOutputStream();

	    DeflaterOutputStream inflater = new DeflaterOutputStream(compressedStream);
	    inflater.write(toCompress, 0, toCompress.length);
	    inflater.close();
		
		return compressedStream.toByteArray();	    
	}
	catch (Exception e)
	{
	    e.printStackTrace();
		return null;
	}
}


Java Decompression:
private byte[] inflaterDecompress(byte[] toDecompress)
{
	try
	{		
	    ByteArrayOutputStream uncompressedStream = new ByteArrayOutputStream();
	    ByteArrayInputStream compressedStream = new ByteArrayInputStream(toDecompress);
	    InflaterInputStream inflater = new InflaterInputStream(compressedStream);
		
	    int c;
	    while ((c = inflater.read()) != -1)
	    {
			uncompressedStream.write(c);
	    }
		
		return uncompressedStream.toByteArray();	    
	}
	catch (Exception e)
	{
	    e.printStackTrace();
		return null;
	}
}

Was This Post Helpful? 0
  • +
  • -

#8 Skick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-April 10

Re: Compression and decompression between Java and C#

Posted 18 August 2011 - 05:49 PM

Nevermind I was doing something wrong. It doesn't work at all. Does anyone know any compression/decompression libraries/methods that are compatible with both C# and Java?
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Compression and decompression between Java and C#

Posted 18 August 2011 - 07:10 PM

Don't know about C# but I have exchange zip files between Windows Winzip and Java (in both ways) using ZipFile, ZipInputStrean (to read files created by Winzip) and ZipOutputStream (to write files readable by Winzip)

Create a Zipfile with Winzip and see if you can read it from both Java and C#.
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Compression and decompression between Java and C#

Posted 18 August 2011 - 07:51 PM

This creates 2 files int a .zip files both readable from Winzip and Winrar ... tested

I'll write the program that reads them from Java
and I'll make a tutorial out of it :)

import java.io.*;
import java.util.zip.*;
/*
 * Creates a ZIP file to be read by Winzip or Winrar
 */
public class WriteZip {

	public static void main(String[] args) {
		
		// lets put 2 files
		String[] filename = {"file1.dat", "file2.dat"};
		// create a line of text for the files
        byte[] buffer = new byte[27];        // the alphabet + <line feed?
        byte letter = 'a';
        for(int i = 0; i < 26; ++i)
        	buffer[i] = letter++;
        buffer[26] = '\n';   // and an end of line
        
        try {
		    // Create the ZIP file
		    String outFilename = "myzip.zip";
		    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));

		    // Compress two files
		    for (int i=0; i<filename.length; i++) {
		        // Add ZIP entry to output stream.
		        out.putNextEntry(new ZipEntry(filename[i]));

		        // Transfer 10 lines into the file
		        for(int j = 0; j < 10; ++j)
		            out.write(buffer);

		        // Close the file
		        out.closeEntry();
		    }

		    // Close the ZIP file
		    out.close();
		} catch (IOException e) {
			System.out.println("Problem writing ZIP file: " + e);
		}

	}
}


Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Compression and decompression between Java and C#

Posted 18 August 2011 - 08:10 PM

And here is your quick and dirty reader

Works between Windows Winzip or Winrar and Java

Now, sorry but can't help you with your C# problem

import java.io.*;
import java.util.zip.*;

public class ReadZip {

	public static void main(String[] args) {
		try {
			// Open the ZIP file
			String inFilename = "myzip.zip";
			ZipInputStream in = new ZipInputStream(new FileInputStream(inFilename));

			// While we have other entry
			ZipEntry entry = in.getNextEntry();
			while(entry != null){
				System.out.println("Reading: " + entry.getName());
				// Transfer bytes from the ZIP file to the output file
				// OK I know that my file contains 10 * 27 bytes the reading might
				// be more versatile and test if there is more bytes in the file
				byte[] buf = new byte[1024];
				
				int len = in.read(buf);
	            String theFile = new String(buf, 0, len);
				System.out.println(theFile);

				entry = in.getNextEntry();
			} // end while
			// Close the ZIP file
			in.close();
		} catch (IOException e) {
			System.out.println("Probel reading back the ZIP file: " + e);
		}

	}
}


Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Compression and decompression between Java and C#

Posted 18 August 2011 - 08:22 PM

And here it is:

http://www.dreaminco...file-from-java/

That was a quick one

Now try to read the myzip.zip from C#. If it does not work, as Winzip and Winrar read it, post your problem in C# forum

http://www.dreaminco...ums/forum/84-c/

This post has been edited by pbl: 18 August 2011 - 08:28 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1