java - Android 上的文件“即时”解包

标签 java android packaging

我需要打包一些文件(总大小不超过 4 GB),这些文件可以在线获取。 Android 应用程序需要“即时”下载此文件,而不将存档保存到设备上。因此基本上该设备不会保存存档然后解压,因为它需要双倍的空间。我应该选择哪种包格式来支持它(例如 zip、tar.gz 等)?

最佳答案

使用.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();
}

关于java - Android 上的文件“即时”解包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25015621/

相关文章:

java - 您必须添加未实现的方法吗?

java - 如何测试 AtomicBoolean 的原子性?

android - 在 Phonegap (windows) 中找不到脚本文件 "C:\res\windows\zip.js"

c - linux中应用程序的自动打包

java - Struts 应用程序的 html 中的额外空间从何而来?

java - 如何在 Java 中获取 URL 对象内路径的父级?

java - 用模式示例替换 if else 语句

c# - 有没有办法以编程方式在 android 中创建布局的副本?

linux - 如何创建在每个发行版上运行的静态二进制文件?