2

in code

GZIPInputStream gzis= new GZIPInputStream(bais);
byte[] bBodyUnzipped= new byte[10240];
gzis.read(bBodyUnzipped);

, how can I optimize the disk space usage and not create a big byte[] by knowing the file unzipped length?

According to this answer there is not such method.

The idea is to use this byte[] for calling

CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
String sBodyUnzipped= decoder.decode(ByteBuffer.wrap(bBodyUnzipped)).toString();

For this reason I need a bytye[] with all the content and no extra zeroes.

Community
  • 1
  • 1
Andreu Alcon
  • 217
  • 3
  • 13

6 Answers6

1

Read into a smaller byte array.

Oswald
  • 31,254
  • 3
  • 43
  • 68
1

Can't you just use Apache commons IOUtils?

Bob Flannigon
  • 1,204
  • 9
  • 8
0

If zip contains binary info you can process it byte by byte

    InputStream is = new BufferedInputStream(new GZIPInputStream(
            new FileInputStream("zip")));
    for (int b; (b = is.read()) != -1;) {
        // process byte
    }

if zip is text then process it line by line, eg

    Scanner sc = new Scanner(new GZIPInputStream(new FileInputStream("zip")));
    while(sc.hasNextLine()) {
        String line = sc.nextLine();
        // process line
    }
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

I think that you want this:

    public void gzip(String path) {
            GZIPInputStream in = null;
            try {
                in = new GZIPInputStream(
                        new FileInputStream(new File(path)));
                byte[] read = new byte[in.available()];
                in.read(read);
                System.out.println(read);
            }catch (Exception e) {
                System.out.println(e);
            }
            finally {
                try {
                    in.close();
                }catch (Exception e) {
                    System.out.println(e);
                }
            }
        }

see: http://docs.oracle.com/javase/6/docs/api/java/io/FileInputStream.html for more info

  • 1
    Yes, this is what a would like. But method in.available is overriden by class InflaterInputStream [(see java doc)](http://docs.oracle.com/javase/6/docs/api/java/util/zip/InflaterInputStream.html#available%28%29), that returns only 0 or 1. – Andreu Alcon Apr 18 '13 at 07:36
  • ok. i always use this approach because i thought that it was equal to the one used for fileInputStream. thanks by the advice. Maybe using a loop we could fix this, since in.read gives us some useful info. I'll try it, if you already found a to solve make me know, please – Ludijor Barros May 08 '13 at 08:13
  • /** * Returns 0 after EOF has been reached, otherwise always return 1. *

    * Programs should not count on this method to return the actual number * of bytes that could be read without blocking. * * return 1 before EOF and 0 after EOF. * exception IOException if an I/O error occurs. * */ public int available() throws IOException {

    – Matthew I. Jun 19 '22 at 20:12
0

I have not found a way of reading all content at once. The alternative is to read by blocks:

    private static String unzip(GZIPInputStream gzis) {
    CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
    byte[] bBodyUnzipped= new byte[1024];
    String sBodyUnzipped= null;
    int offset= 0;
    int bodyLength= 0;
    do {
        bodyLength= gzis.read(bBodyUnzipped, offset, 1024);
        sBodyUnzipped+= decoder.decode(ByteBuffer.wrap(bBodyUnzipped, 0, bodyLength)).toString();
        offset+= bodyLength;
    } while(bodyLength < 0);
    return sBodyUnzipped;
}
Andreu Alcon
  • 217
  • 3
  • 13
0
public  byte[] readGZFile(File file) {

    byte[] fileData = null;
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPInputStream in = null;
    try {
        in = new GZIPInputStream(new FileInputStream(file));
        int bufsize=1024;
        byte [] buf=new byte[bufsize];
        int readbytes=0;
        readbytes=in.read(buf);
        while(readbytes!=-1){
            baos.write(buf, 0,readbytes);
            readbytes=in.read(buf);
        }
        baos.flush();
        return baos.toByteArray();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return fileData;
}
karimvai
  • 319
  • 3
  • 14