Android:读取/写入 ZipInputStream 的更快方法?

发布于 2024-11-26 19:55:22 字数 1404 浏览 2 评论 0原文

我目前正在编写一个应用程序,该应用程序读取资产文件夹中的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

山人契 2024-12-03 19:55:22

我找到了一个更好的解决方案,它实现了 BuffererdOutputStream API。我的解决方案如下所示:

   byte[] buffer = new byte[2048];

   FileOutputStream fout = new FileOutputStream(f);
   BufferedOutputStream bos = new BufferedOutputStream(fout, buffer.length);

   int size;

    while ((size = zin.read(buffer, 0, buffer.length)) != -1) {
            bos.write(buffer, 0, size);
        }
        //Close up shop..
        bos.flush();
        bos.close();

        fout.flush();
        fout.close();
        zin.closeEntry();

我设法将加载时间从平均约 5 分钟增加到约 5 分钟(取决于包中的图像数量)。希望这有帮助!

I found a much better solution which implements the BuffererdOutputStream API. My solution looks like this:

   byte[] buffer = new byte[2048];

   FileOutputStream fout = new FileOutputStream(f);
   BufferedOutputStream bos = new BufferedOutputStream(fout, buffer.length);

   int size;

    while ((size = zin.read(buffer, 0, buffer.length)) != -1) {
            bos.write(buffer, 0, size);
        }
        //Close up shop..
        bos.flush();
        bos.close();

        fout.flush();
        fout.close();
        zin.closeEntry();

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!

酒浓于脸红 2024-12-03 19:55:22

尝试使用 http://commons.apache.org/io/
喜欢:

 InputStream in = new URL( "http://jakarta.apache.org" ).openStream();
 try {
   System.out.println( IOUtils.toString( in ) );
 } finally {
   IOUtils.closeQuietly(in);
 }

Try use http://commons.apache.org/io/
like:

 InputStream in = new URL( "http://jakarta.apache.org" ).openStream();
 try {
   System.out.println( IOUtils.toString( in ) );
 } finally {
   IOUtils.closeQuietly(in);
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文