Android:读取/写入 ZipInputStream 的更快方法?
我目前正在编写一个应用程序,该应用程序读取资产文件夹中的 zip 文件,其中包含一堆图像。我使用 ZipInputStream API 读取内容,然后将每个文件写入我的:Environment.getExternalStorageDirectory() 目录。我一切正常,但第一次运行应用程序时,将图像写入存储目录的速度非常慢。将图像写入光盘大约需要 5 分钟。我的代码如下所示:
ZipEntry ze = null;
ZipInputStream zin = new ZipInputStream(getAssets().open("myFile.zip"));
String location = getExternalStorageDirectory().getAbsolutePath() + "/test/images/";
//Loop through the zip file
while ((ze = zin.getNextEntry()) != null) {
File f = new File(location + ze.getName());
//Doesn't exist? Create to avoid FileNotFoundException
if(f.exists()) {
f.createNewFile();
}
FileOutputStream fout = new FileOutputStream(f);
//Read contents and then write to file
for (c = zin.read(); c != -1; c = zin.read()) {
fout.write(c);
}
}
fout.close();
zin.close();
读取特定条目的内容然后写入的过程非常慢。我认为这更多地与阅读有关,而不是与写作有关。我读过您可以使用 byte[]
数组缓冲区来加速该过程,但这似乎不起作用!我尝试了这个,但它只读取了文件的一部分...
FileOutputStream fout = new FileOutputStream(f);
byte[] buffer = new byte[(int)ze.getSize()];
//Read contents and then write to file
for (c = zin.read(buffer); c != -1; c = zin.read(buffer)) {
fout.write(c);
}
}
当我这样做时,我只写入了大约 600-800 字节。有没有办法加快速度?我是否错误地实现了缓冲区数组?
I am currently writing an application that reads a zip file in my assets folder which contains a bunch of images. I am using the ZipInputStream
API to read the contents and then writing each file to my: Environment.getExternalStorageDirectory()
directory. I have everything working but the first time the application is run writing the images to the storage directory is INCREDIBLY slow. It takes about about 5 minutes to write my images to disc. My code looks like this:
ZipEntry ze = null;
ZipInputStream zin = new ZipInputStream(getAssets().open("myFile.zip"));
String location = getExternalStorageDirectory().getAbsolutePath() + "/test/images/";
//Loop through the zip file
while ((ze = zin.getNextEntry()) != null) {
File f = new File(location + ze.getName());
//Doesn't exist? Create to avoid FileNotFoundException
if(f.exists()) {
f.createNewFile();
}
FileOutputStream fout = new FileOutputStream(f);
//Read contents and then write to file
for (c = zin.read(); c != -1; c = zin.read()) {
fout.write(c);
}
}
fout.close();
zin.close();
The process of reading the contents of the particular entry and then writing to it is VERY slow. I am assuming it is more to do with reading than writing. I've read that you can use a byte[]
array buffer to speed up the process but this does not seem to work! I tried this but it only read part of the file...
FileOutputStream fout = new FileOutputStream(f);
byte[] buffer = new byte[(int)ze.getSize()];
//Read contents and then write to file
for (c = zin.read(buffer); c != -1; c = zin.read(buffer)) {
fout.write(c);
}
}
When I do that I only get about 600-800 bytes written. Is there a way to speed this up?? Have I implemented the buffer array incorrectly??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了一个更好的解决方案,它实现了 BuffererdOutputStream API。我的解决方案如下所示:
我设法将加载时间从平均约 5 分钟增加到约 5 分钟(取决于包中的图像数量)。希望这有帮助!
I found a much better solution which implements the
BuffererdOutputStream
API. My solution looks like this:I managed to increase my load time from anywhere from an average of about 5 minutes to about 5 (depending on how many images are in the package). Hope this helps!
尝试使用 http://commons.apache.org/io/
喜欢:
Try use http://commons.apache.org/io/
like: