Results 1 to 3 of 3
  1. #1
    NeuroFuzzy is offline Member
    Join Date
    Sep 2010
    Posts
    10
    Rep Power
    0

    Default Decompress (un-gzip) a byte[]?

    I'm getting a byte array from the interwebz. However, this information is GZIP compressed.

    Because I'm getting the info from the internet, I really don't want to save it and then read the information, so that rules out FileInputStream/GZIPInputStream. With the "Inflater" class, it says that to unzip a GZIP file, you need a "dummy byte", but it doesn't elaborate at all. Adding a 0 byte to the beginning or end doesn't seem to help. This is the code I'm using:
    Java Code:
    	public static byte[] unzipByteArray(byte[] file) throws IOException 
    	{
    		byte[] byReturn = null;
    		Inflater oInflate = new Inflater(true);
    		oInflate.setInput(file);
    		ByteArrayOutputStream oZipStream = new ByteArrayOutputStream();
    		try 
    		{
    			while (! oInflate.finished() )
    			{
    				byte[] byRead = new byte[ZIP_BUFFER_SIZE];
    				int iBytesRead = oInflate.inflate(byRead);
    				if (iBytesRead == byRead.length)
    				{
    					oZipStream.write(byRead);
    				} else {
    					oZipStream.write(byRead, 0, iBytesRead);
    				}
    			}
    			byReturn = oZipStream.toByteArray();
    		} catch (DataFormatException ex) {
    			throw new IOException("Attempting to unzip file that is not zipped.");
    		} finally {
    			oZipStream.close();
    		}
    		return byReturn;
    	}




    So can anyone tell me how I can un-GZIP a byte array? Either saying where I can put the dummy byte, or what other solution I can use? Right now I'm just planning on chewing through the zlib docs.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Thats a very good question. I would have assumed the same thing - adding one to the beginning or to the end, but you say you had no luck? Have you done like the example shows and try compressing a know string with deflator (nowrap) and decompressing it again with inflator (nowrap)? I would see if I could get this working first before continuing with the bytes from the web - the issue might be related to the means by which the file was compressed.

  3. #3
    NeuroFuzzy is offline Member
    Join Date
    Sep 2010
    Posts
    10
    Rep Power
    0

    Default

    ARGHPHHHhhhhh

    Well, when I save the compressed byte array to my hard drive, I can open it with winrar and decompress the file (decompresses fine, just as expected). If I save a byte array that isn't compressed, using a GZIPOutputStream, then I can load it back in and decompress the information.

    This would hint that the file isn't a gzip file... but all the documentation I've read says it is! Either GZIP is a bigger umbrella term than I (or whoever wrote the class) realize, and this is a "different kind" of gzip, OR it really isn't a gzip file.

    :\

    [edit]

    test code:
    Java Code:
    import java.io.*;
    import java.util.zip.*;
    
    public class gzipCompression 
    {
    	private static final int ZIP_BUFFER_SIZE = 2048;
    	public static void main(String[] args)
    	{
    		byte[] originalData;
    		byte[] loadedData;
    		byte[] ungzipData=new byte[0];
    		
    		
    		String dir="C:\\temp\\";
    		String myFile="temp.txt";
    		File temptxt=new File(dir+myFile);
    		
    		String myString="Hello, I'm a string."; //populate originalData with the myString.
    		char[] n=myString.toCharArray();
    		originalData=new byte[n.length];
    		for(int count=0;count<n.length;count++)
    			originalData[count]=(byte)n[count];
    		try
    		{
    			FileOutputStream fos=new FileOutputStream(temptxt); //gzip the text and save the file
    			GZIPOutputStream zos=new GZIPOutputStream(new BufferedOutputStream(fos));
    			zos.write(originalData);
    			zos.close();
    			
    			FileInputStream fis=new FileInputStream(temptxt);
    			
    			
    			loadedData=new byte[(int)temptxt.length()];
    			fis.read(loadedData);
    			fis.close();
    			ungzipData=unzipByteArray(loadedData);
    		}
    		catch(Exception e){}
    		System.out.println(temptxt);
    		for(int count=0;count<ungzipData.length;count++)
    			n[count]=(char)ungzipData[count];
    		System.out.println(new String(n));
    		
    	}
    		public static byte[] unzipByteArray(byte[] file) throws IOException 
    		{
    			byte[] byReturn = null;
    			Inflater oInflate = new Inflater(true);
    			oInflate.setInput(file);
    			ByteArrayOutputStream oZipStream = new ByteArrayOutputStream();
    			try 
    			{
    				while (! oInflate.finished() )
    				{
    					byte[] byRead = new byte[ZIP_BUFFER_SIZE];
    					int iBytesRead = oInflate.inflate(byRead);
    					if (iBytesRead == byRead.length)
    					{
    						oZipStream.write(byRead);
    					} else {
    						oZipStream.write(byRead, 0, iBytesRead);
    					}
    				}
    				byReturn = oZipStream.toByteArray();
    			} catch (DataFormatException ex) {
    				throw new IOException("Attempting to unzip file that is not zipped.");
    			} finally {
    				oZipStream.close();
    			}
    			return byReturn;
    		}
    }

Similar Threads

  1. Read a bitmap byte by byte
    By cbr400aero in forum Java 2D
    Replies: 2
    Last Post: 10-19-2010, 12:42 AM
  2. byte[]
    By bobo67 in forum New To Java
    Replies: 10
    Last Post: 09-11-2010, 03:06 PM
  3. Replies: 6
    Last Post: 08-10-2010, 10:22 AM
  4. Replies: 1
    Last Post: 04-21-2010, 03:28 AM
  5. gZIp decompression with j2se
    By ashakthi84 in forum Networking
    Replies: 1
    Last Post: 12-25-2007, 05:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •