'在飞行中'在Android上解压缩文件

时间:2014-07-29 12:43:18

标签: java android packaging

我需要打包几个文件(总大小达4 GB),这些文件将在线提供。一个Android应用程序需要动态下载这个'不将存档保存到设备上。所以基本上设备不会保存存档然后解压缩,因为它需要两倍的空间。我应该选择哪种包格式支持它(例如zip,tar.gz等)?

1 个答案:

答案 0 :(得分:1)

使用.zip!您可以使用ZipInputStreamZipOutputStream即时读取和写入.zip文件。无需从存档中提取文件。

Link to documentation


这是一个简单的例子:

InputStream is =...
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is));
try {

    // ZipEntry contains data about files and folders in the archive.
    ZipEntry ze; 

    // This loops through the whole content of the archive
    while ((ze = zis.getNextEntry()) != null) {

        // Here we read the whole data of one ZipEntry
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int count;
        while ((count = zis.read(buffer)) != -1) {
            baos.write(buffer, 0, count);
        }

        // The ZipEntry contains data about the file, like its filename
        String filename = ze.getName();

        // And that's the file itself as byte array
        byte[] bytes = baos.toByteArray();

        // Do something with the file            
    }
} finally {
    zis.close();
}