2010-09-20 11 views
8

So komprimieren und dekomprimieren Sie eine Datei in Android mit GZip. Bitte geben Sie etwas an, damit es mir eine große Hilfe ist.GZip in Android

Vielen Dank im Voraus

+0

siehe diesen Link http://stackoverflow.com/q/6717165/779408. Eine Komprimierungs- und Dekomprimierungsmethode wird dort dargestellt. – breceivemail

Antwort

3
+2

Bitte geben Sie mir einige Echtzeit-Beispiele ... –

+3

Sie benötigen Beispiele, wie Sie Streams verwenden können? –

+0

Vielen Dank für Ihre tolle Antwort Freunde, fand ich Lösung für meinen eigenen Beitrag. –

2

Ich stieß vor einiger Zeit auf dasselbe Problem. Hier werden die Funktionen i

Compress Funktion

if (responseCode == HttpConnection.HTTP_OK){

boolean stop = false, pause = false; 
totalSize = conn.getLength() + downloaded; 
chunkSize = (int)(conn.getLength()/100); 
System.out.println("*********-----" + conn.getLength() + ""); 
System.out.println("-----------------ok"); 
in = conn.openInputStream(); 
int length = 0, s = 0; 
byte[] readBlock = new byte[(int)conn.getLength()]; 


while ((s = in.read(readBlock) != -1) 
     length = length + s; 
{ 
     if (!pause) 
     { 
      readBlock = Decompress.decompress(readBlock); 
      out.write(readBlock, 0, length); 
      downloaded += length; 
      int a = getPerComplete(totalSize, downloaded); 
      System.out.println("% OF Downloaded--------" + a); 
      int a1 = getPerComplete(totalSize, downloaded); 

Dekomprimieren-Funktion: -

public byte[] decompress(byte[] compressed) throws IOException 
{ 
    GZIPInputStream gzipInputStream; 
    if (compressed.length > 4) 
    { 
     gzipInputStream = new GZIPInputStream(
      new ByteArrayInputStream(compressed, 4, 
            compressed.length - 4)); 

     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     for (int value = 0; value != -1;) 
     { 
      value = gzipInputStream.read(); 
      if (value != -1) 
      { 
       baos.write(value); 
      } 
     } 
     gzipInputStream.close(); 
     baos.close(); 

     return baos.toByteArray(); 
    } 
    else 
    { 
     return null; 
    } 
} 

}

13

Bitte verwenden Sie die folgenden Methoden, um die Zeichenfolge zu komprimieren mit gzip.

public static byte[] compress(String string) throws IOException { 
    ByteArrayOutputStream os = new ByteArrayOutputStream(string.length()); 
    GZIPOutputStream gos = new GZIPOutputStream(os); 
    gos.write(string.getBytes()); 
    gos.close(); 
    byte[] compressed = os.toByteArray(); 
    os.close(); 
    return compressed; 
} 

public static String decompress(byte[] compressed) throws IOException { 
    final int BUFFER_SIZE = 32; 
    ByteArrayInputStream is = new ByteArrayInputStream(compressed); 
    GZIPInputStream gis = new GZIPInputStream(is, BUFFER_SIZE); 
    StringBuilder string = new StringBuilder(); 
    byte[] data = new byte[BUFFER_SIZE]; 
    int bytesRead; 
    while ((bytesRead = gis.read(data)) != -1) { 
     string.append(new String(data, 0, bytesRead)); 
    } 
    gis.close(); 
    is.close(); 
    return string.toString(); 
}