Android 解压缩 zip文件的速度问题 工作中遇到下载zip包到SDCard卡,然后使用ZipInputStream解压缩,解压没有问题,但是解压速度太慢了,文件有11M多,解压时间超长,如果需要解压的包更多的话担心程序会跑死。大家有什么更好的解决方法吗?Android压缩zip速度 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 不行就起个线程放后台解去吧,让用户先干点别的,解完了发个handler告诉主线程可以用了。。 public class UnZip { private Handler mHandler; public UnZip(Handler mHandler) { this.mHandler = mHandler; } public void unZipOffline(String from, String to) throws IOException { File fromFile = new File(from); File toFile = new File(to); FileInputStream fInputS = new FileInputStream(fromFile); ZipInputStream zipInput = new ZipInputStream(fInputS); // 获取ZipInputStream中的ZipEntry条目,一个zip文件中可能包含多个ZipEntry, // 当getNextEntry方法的返回值为null,则代表ZipInputStream中没有下一个ZipEntry, // 输入流读取完成; ZipEntry entry = zipInput.getNextEntry(); File file; entry = zipInput.getNextEntry(); while (entry != null) { if(entry.isDirectory()) { String name = entry.getName(); name = name.substring(0, name.length() - 1); file = new File(toFile.getPath() + File.separator + name); boolean a = file.mkdirs(); } else { file = new File(toFile.getPath() + File.separator + entry.getName()); file.createNewFile(); FileOutputStream out = new FileOutputStream(file); int b; while ((b = zipInput.read()) != -1) { out.write(b); } out.close(); } entry = zipInput.getNextEntry(); } mHandler.sendEmptyMessage(0); } /** * 解压缩一个文件 * * @param zipFile 压缩文件 * @param folderPath 解压缩的目标目录 * @throws IOException 当解压缩过程出错时抛出 */ public void upZipFile(String zipPath, String folderPath) throws ZipException, IOException { File zipFile = new File(zipPath); File desDir = new File(folderPath); if (!desDir.exists()) { desDir.mkdirs(); } ZipFile zf = new ZipFile(zipFile); for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements();) { ZipEntry entry = ((ZipEntry)entries.nextElement()); InputStream in = zf.getInputStream(entry); String str = folderPath + File.separator + entry.getName(); File desFile = new File(str); if (!desFile.exists()) { File fileParentDir = desFile.getParentFile(); if (!fileParentDir.exists()) { fileParentDir.mkdirs(); } desFile.createNewFile(); } OutputStream out = new FileOutputStream(desFile); byte buffer[] = new byte[4096]; int realLength; while ((realLength = in.read(buffer)) > 0) { out.write(buffer, 0, realLength); } in.close(); out.close(); } mHandler.sendEmptyMessage(0); }这两个方法解压效率差别很大,第二个很快就好了,请教下这是为什么 方法一 while ((b = zipInput.read()) != -1) { out.write(b); }一个byte一个byte地写方法二byte buffer[] = new byte[4096]; int realLength; while ((realLength = in.read(buffer)) > 0) { out.write(buffer, 0, realLength); }4KB、4KB地写效率自然差得远 Android 调用第三方分享程序 bmp图片透明色的问题 请问安装android sdk manager,下载速度为什么这么慢? Barnacle Wifi Tether 有人研究过吗? android模拟器太卡了 Android测试开发工程师 VS Android开发工程师 [Android] 怎样把一些文字和图片信息转化为PDF文件? android背景图片的处理 新手求助--ListView与Button无法共存 videoview在线播放优酷M3U8视频,暂停不好用,暂停之后还会自动开始 WebView第一次加载会白屏,求解决方案 A10开发板 热插拔切换hdmi和vga问题
public class UnZip {
private Handler mHandler;
public UnZip(Handler mHandler) {
this.mHandler = mHandler;
}
public void unZipOffline(String from, String to) throws IOException {
File fromFile = new File(from);
File toFile = new File(to);
FileInputStream fInputS = new FileInputStream(fromFile);
ZipInputStream zipInput = new ZipInputStream(fInputS);
// 获取ZipInputStream中的ZipEntry条目,一个zip文件中可能包含多个ZipEntry,
// 当getNextEntry方法的返回值为null,则代表ZipInputStream中没有下一个ZipEntry,
// 输入流读取完成;
ZipEntry entry = zipInput.getNextEntry();
File file;
entry = zipInput.getNextEntry();
while (entry != null) {
if(entry.isDirectory()) {
String name = entry.getName();
name = name.substring(0, name.length() - 1);
file = new File(toFile.getPath() + File.separator + name);
boolean a = file.mkdirs();
} else {
file = new File(toFile.getPath() + File.separator + entry.getName());
file.createNewFile();
FileOutputStream out = new FileOutputStream(file);
int b;
while ((b = zipInput.read()) != -1) {
out.write(b);
}
out.close();
}
entry = zipInput.getNextEntry();
}
mHandler.sendEmptyMessage(0);
}
/**
* 解压缩一个文件
*
* @param zipFile 压缩文件
* @param folderPath 解压缩的目标目录
* @throws IOException 当解压缩过程出错时抛出
*/
public void upZipFile(String zipPath, String folderPath) throws ZipException, IOException {
File zipFile = new File(zipPath);
File desDir = new File(folderPath);
if (!desDir.exists()) {
desDir.mkdirs();
}
ZipFile zf = new ZipFile(zipFile);
for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements();) {
ZipEntry entry = ((ZipEntry)entries.nextElement());
InputStream in = zf.getInputStream(entry);
String str = folderPath + File.separator + entry.getName();
File desFile = new File(str);
if (!desFile.exists()) {
File fileParentDir = desFile.getParentFile();
if (!fileParentDir.exists()) {
fileParentDir.mkdirs();
}
desFile.createNewFile();
}
OutputStream out = new FileOutputStream(desFile);
byte buffer[] = new byte[4096];
int realLength;
while ((realLength = in.read(buffer)) > 0) {
out.write(buffer, 0, realLength);
}
in.close();
out.close();
}
mHandler.sendEmptyMessage(0);
}这两个方法解压效率差别很大,第二个很快就好了,请教下这是为什么
out.write(b);
}
一个byte一个byte地写方法二byte buffer[] = new byte[4096];
int realLength;
while ((realLength = in.read(buffer)) > 0) {
out.write(buffer, 0, realLength);
}4KB、4KB地写效率自然差得远