从Android资产文件夹中的ZIP文件读取文件
我正在使用从Android资产文件夹中的ZIP文件读取文件,android,zip,assets,Android,Zip,Assets,我正在使用ZipInputStream从位于我的Android资产文件夹中的ZIP文件中读取文件:它可以工作,但速度非常慢,因为它必须使用getnextery()按顺序读取,而且有相当多的文件 如果我将ZIP文件复制到SD卡上,使用ZipFile.getEntry时读取速度非常快,但我没有找到将ZipFile与资产文件一起使用的方法 有没有办法快速访问资产文件夹中的ZIP文件?还是我真的要把拉链复制到SD卡上 (顺便说一句,如果有人想知道我为什么这么做:这个应用程序大于50MB,所以为了在Pla
ZipInputStream
从位于我的Android资产文件夹中的ZIP文件中读取文件:它可以工作,但速度非常慢,因为它必须使用getnextery()
按顺序读取,而且有相当多的文件
如果我将ZIP文件复制到SD卡上,使用ZipFile.getEntry
时读取速度非常快,但我没有找到将ZipFile
与资产文件一起使用的方法
有没有办法快速访问资产文件夹中的ZIP文件?还是我真的要把拉链复制到SD卡上
(顺便说一句,如果有人想知道我为什么这么做:这个应用程序大于50MB,所以为了在Play Store中使用它,我必须使用扩展APK;但是,由于这个应用程序也应该放在Amazon应用程序商店中,所以我必须使用另一个版本,因为Amazon不支持扩展APK,很自然……我认为访问ZIP文件是一个很好的选择。)t两个不同的位置将是处理此问题的一种简单方法,但遗憾的是……您可以通过以下方式创建ZipInputStream:
ZipInputStream zipIs = new ZipInputStream(context.getResources().openRawResource(your.package.com.R.raw.filename));
ZipEntry ze = null;
while ((ze = zipIs.getNextEntry()) != null) {
FileOutputStream fout = new FileOutputStream(FOLDER_NAME +"/"+ ze.getName());
byte[] buffer = new byte[1024];
int length = 0;
while ((length = zipIs.read(buffer))>0) {
fout.write(buffer, 0, length);
}
zipIs .closeEntry();
fout.close();
}
zipIs .close();
您可以将未压缩的文件直接存储在资产中(即,将压缩包解压到资产/文件夹中)。这样,您可以直接访问这些文件,并且在构建APK时,这些文件将被压缩。这对我很有用:
private void loadzip(String folder, InputStream inputStream) throws IOException
{
ZipInputStream zipIs = new ZipInputStream(inputStream);
ZipEntry ze = null;
while ((ze = zipIs.getNextEntry()) != null) {
FileOutputStream fout = new FileOutputStream(folder +"/"+ ze.getName());
byte[] buffer = new byte[1024];
int length = 0;
while ((length = zipIs.read(buffer))>0) {
fout.write(buffer, 0, length);
}
zipIs.closeEntry();
fout.close();
}
zipIs.close();
}
谢谢,但正如我所写的,我已经使用了一个
ZipInputStream
,但是使用getnextry()在ZIP中查找
太慢了!你用缓冲区读取文件吗?我给我的代码提供了一个例子。对我来说效果很好。不是提取太慢,而是在zip文件中搜索正确的文件。ZipFile.getEntry(filename)
类似于ZipInputStream
吗?