GZIP packen, entpacken

  • 1 Antworten
  • Letztes Antwortdatum
R

r4in

Ambitioniertes Mitglied
3
Hi Leute,

ich packe mir mit dem GZIP Algorithmus einen String, funktioniert auch soweit. Wenn ich diesen String aber wieder entpacken möchte bekomme ich die Exception: "unknown format (magic number 0)"

Jemand eine Idee woran das liegen kann?

Code:
public String packen() {

        
        String text_in = "bla bla irgendwas";
        

        System.out.println("ungezippte Laenge = " + text_in.length());
        
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        GZIPOutputStream gzip = null;
        try {
            gzip = new GZIPOutputStream(out);
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
            gzip.write(text_in.getBytes());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            gzip.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        System.out.println("gezippte Laenge = " + out.toString().length());
        
        try {
            return out.toString("ISO-8859-1");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return text_in;
    
    }


/*
*    bekommt den gezippten String mit .getBytes() übergeben
*/ 
public String entpacken (byte[] compressed) throws IOException
    {
        
        int size = 0;
        byte[] gzipBuff = compressed;

        ByteArrayInputStream memstream = new      ByteArrayInputStream(gzipBuff, 4, gzipBuff.length - 4);
        GZIPInputStream gzin = new GZIPInputStream(memstream);

        final int buffSize = 8192;
        byte[] tempBuffer = new byte[buffSize];
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        while ((size = gzin.read(tempBuffer, 0, buffSize)) != -1) {
            baos.write(tempBuffer, 0, size);
        }
        byte[] buffer = baos.toByteArray();
        baos.close();

        return new String(buffer, "UTF-8");
        
    }
 
Wo genau bekommst du die Exception?
Und warum fängst du bei dem Stream mit dem offset 4 an?
 
Zurück
Oben Unten