Android 安卓中的 GZip

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3752359/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:39:48  来源:igfitidea点击:

GZip in android

androidgzipgzipoutputstream

提问by Nandagopal T

How to compress and decompress a file in android using GZip. please provide with some reference , so that it would a great help for me.

如何使用 GZip 在 android 中压缩和解压缩文件。请提供一些参考,以便对我有很大帮助。

Thanks in advance

提前致谢

回答by Vyshnavi

Please use the following methods to compress the string using gzip.

请使用以下方法使用 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();
}

回答by mreichelt

回答by shashankkb

I ran into same problem some time ago. Here are the functions i used

前段时间我遇到了同样的问题。这是我使用的功能

Compress Function

压缩功能

if (responseCode == HttpConnection.HTTP_OK){

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);

Decompress function:-

解压功能:-

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;
    }
}

}

}