Android 中的 GZip

发布于 2024-09-24 07:48:41 字数 68 浏览 0 评论 0原文

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

提前致谢

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.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黑白记忆 2024-10-01 07:48:41

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

Please use the following methods to compress the string using 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();
}
是你 2024-10-01 07:48:41

我前段时间遇到了同样的问题。以下是我使用的函数

压缩函数

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

解压缩函数:-

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

}

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

Compress Function

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

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文