我需要打包一些文件(总大小不超过 4 GB),这些文件可以在线获取。 Android 应用程序需要“即时”下载此文件,而不将存档保存到设备上。因此基本上该设备不会保存存档然后解压,因为它需要双倍的空间。我应该选择哪种包格式来支持它(例如 zip、tar.gz 等)?
最佳答案
使用.zip!您可以使用 ZipInputStream
和 ZipOutputStream
即时读取和写入 .zip 文件。无需从存档中提取文件。
这是一个简单的例子:
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/