• Post Reply Bookmark Topic Watch Topic
  • New Topic

HELP - GZIPInputStream

 
Dave Donohoe
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
In my code I write to a file using GZIPInputStream. Now I want to read the contents of this back into a byte array. When I use myGZip.available() to specify the size of my array, it only reads in the first char in the file - does anyone know why?
or does anyone know how I can specify the size of my byte array without using available()?
Thanks in advance,
D
 
Bryan Fagan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/D
GZIPInputStream::available() overrides InputStream::available(). In fact GZIPInputStream::available() returns 0 if you are at the EOF or 1 if everything is OK, thus the reason your byte array consists of one char. You have to make sure to use InputStream::available() and not GZIPInputStream::available.
It's InputStream::availble() that actually tells you how many bytes are available.
-Bryan :-)
Originally posted by Dave Donohoe:
Hi all,
In my code I write to a file using GZIPInputStream. Now I want to read the contents of this back into a byte array. When I use myGZip.available() to specify the size of my array, it only reads in the first char in the file - does anyone know why?
or does anyone know how I can specify the size of my byte array without using available()?
Thanks in advance,
D

 
Bryan Fagan
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To add to what I said earlier, because we don't know how big a file might be, it's probably better to use a Collection like an java.util.ArrayList. An ArrayList can grow as needed.
Note: I used a regular Zip Format and not the GZIP format.
Here is some code below describing what I mean.
/**
* Test reading from a zip file and creating a Byte array
*
*/
import java.io.*;
import java.util.*;
import java.util.zip.*;
public class RZip extends ZipInputStream
{
private ArrayList aByteArray;
private int len;
private InputStream zin;
public RZip( InputStream ins ) throws IOException
{
super( ins );
zin = this.in;
aByteArray = new ArrayList();
}
/**
* Create an array of bytes with ArrayList Collection.
*/
public void createArrayOfBytes() throws IOException
{

int len = 0;
while( ( len = zin.read() ) != -1 )
{
aByteArray.add( new Byte( ( byte )len ) );
}
}
/**
* print out array of bytes.
*/
public void printOutArrayOfBytes()
{
for( int i=0;i<aByteArray.size();i++ )>
{
System.out.println("Byte: " + aByteArray.get( i ).toString() );
}
}
public static void main(String[] args)
{
try
{
ZipFile zFile = new ZipFile( "d:\\java\\test\\Test.zip" );
ZipEntry zEntry = zFile.getEntry( "Test.txt" );
InputStream i = zFile.getInputStream( zEntry );

RZip rZip = new RZip( i );
rZip.createArrayOfBytes();
rZip.printOutArrayOfBytes();
}
catch( Exception e )
{
System.err.println( "MAIN::ERROR: " + e.getMessage() );
System.exit( 1 );
}
}
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!