android 压缩文件快速,Android:更快的方式来读/写ZipInputStream?

我目前正在编写一个应用程序,它在我的assets文件夹中读取一个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字节.有没有办法加快这个?我是否错误地实现了缓冲区数组?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android开发中,压缩文件是一种常见的操作,可以用于将多个文件或文件夹打包成一个压缩文件,或者解压缩已有的压缩文件Android提供了一些类和方法来实现文件的压缩和解压缩。 1. 压缩文件Android中常用的压缩文件格式是ZIP格式,可以使用java.util.zip包中的类来进行压缩操作。以下是一个简单的示例代码: ```java import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; public class ZipUtils { public static void zipFile(String sourceFilePath, String zipFilePath) { try { FileOutputStream fos = new FileOutputStream(zipFilePath); ZipOutputStream zos = new ZipOutputStream(fos); File file = new File(sourceFilePath); FileInputStream fis = new FileInputStream(file); ZipEntry zipEntry = new ZipEntry(file.getName()); zos.putNextEntry(zipEntry); byte[] buffer = new byte[1024]; int length; while ((length = fis.read(buffer)) > 0) { zos.write(buffer, 0, length); } fis.close(); zos.closeEntry(); zos.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 上述代码中的`zipFile`方法接收两个参数,`sourceFilePath`表示要压缩的文件路径,`zipFilePath`表示生成的压缩文件路径。该方法会将指定的文件压缩成一个ZIP文件。 2. 解压缩文件: 同样使用java.util.zip包中的类来进行解压缩操作。以下是一个简单的示例代码: ```java import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; public class UnzipUtils { public static void unzipFile(String zipFilePath, String destDirectory) { try { File destDir = new File(destDirectory); if (!destDir.exists()) { destDir.mkdir(); } ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFilePath)); ZipEntry zipEntry = zis.getNextEntry(); while (zipEntry != null) { String fileName = zipEntry.getName(); File newFile = new File(destDirectory + File.separator + fileName); FileOutputStream fos = new FileOutputStream(newFile); byte[] buffer = new byte[1024]; int length; while ((length = zis.read(buffer)) > 0) { fos.write(buffer, 0, length); } fos.close(); zipEntry = zis.getNextEntry(); } zis.closeEntry(); zis.close(); } catch (Exception e) { e.printStackTrace(); } } } ``` 上述代码中的`unzipFile`方法接收两个参数,`zipFilePath`表示要解压缩的ZIP文件路径,`destDirectory`表示解压缩后的目标文件夹路径。该方法会将指定的ZIP文件解压缩到目标文件夹中。 以上是Android压缩文件和解压缩文件的简单示例代码,你可以根据自己的需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值