android解压zip包问题

微笑俱乐部 2011-11-03 03:17:13
在android手机上解压java网页打的zip包,出现找不到文件问题android手机解压代码
/**
* 使用 org.apache.tools.zip.ZipFile 解压文件,它与 java 类库中的
* java.util.zip.ZipFile 使用方式是一样 的,只不过多了设置编码方式的
* 接口。
*
* 注,apache 没有提供 ZipInputStream 类,所以只能使用它提供的ZipFile
* 来读取压缩文件。
* @param archive 压缩包路径
* @param decompressDir 解压路径
* @throws IOException
* @throws FileNotFoundException
* @throws ZipException
*/

public static void unZip(String archive, String decompressDir)
throws IOException, FileNotFoundException, ZipException {

BufferedInputStream bi;

ZipFile zf = new ZipFile(archive, "GBK");//支持中文
Enumeration e = zf.getEntries();
while (e.hasMoreElements()) {
ZipEntry ze2 = (ZipEntry) e.nextElement();
String entryName = ze2.getName();
System.out.println("入口是:" + entryName);
//String path = decompressDir + "//" + entryName;
String path = decompressDir + entryName;
if (ze2.isDirectory()) {
System.out.println("正在创建解压目录 - " + entryName);
File decompressDirFile = new File(path);
if (!decompressDirFile.exists()) {
decompressDirFile.mkdirs();
}
} else {
System.out.println("正在创建解压文件 - " + entryName);
String fileDir = path.substring(0, path.lastIndexOf("/"));
File fileDirFile = new File(fileDir);
if (!fileDirFile.exists()) {
fileDirFile.mkdirs();
}
System.out.println(decompressDir + entryName);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(decompressDir + "/" + entryName));
//BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(decompressDir + entryName));
bi = new BufferedInputStream(zf.getInputStream(ze2));
byte[] readContent = new byte[1024];
int readCount = bi.read(readContent);
while (readCount != -1) {
bos.write(readContent, 0, readCount);
readCount = bi.read(readContent);
}
bos.close();
}
}
zf.close();
}


java打包代码

/**
* 打成zip压缩包
* @param dirPath 源文件夹路径
* @param toZipPath 生成zip包存放路径 如D://a 或者D://a//
* @param filename 压缩包文件名
* @throws Exception
*/
public static void doZip(String dirPath, String toZipPath,String filename) throws IOException {
File dir = null;
ZipOutputStream zipOut = null;
String zipDirName = ""; //存储生成的zip包的路径
String parentPath = null;
dir = new File(dirPath);
zipDirName = getZipPath(filename, toZipPath);
parentPath = dir.getParent();
zipOut = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipDirName)));
doZipHandlerDir(dir, zipOut, parentPath);
zipOut.close();
System.out.println("success");
}
/**
* 获得zip存储路径
* @param dirName
* @param toZipPath
* @return
*/
private static String getZipPath(String dirName, String toZipPath) {
String zipDirName = "";
if (toZipPath != null && !"".equals(toZipPath.trim())) {
//zipDirName = toZipPath + File.separator;
zipDirName = toZipPath + "/";
File newDir = new File(zipDirName);
if (!newDir.exists()) {
newDir.mkdirs();
}
}
zipDirName += dirName + ".zip";
return zipDirName;
}
/**
* 递归完成目录下文件读取
* @param dir
* @param zipOut
* @throws IOException
* @throws Exception
*/
private static void doZipHandlerDir(File dir, ZipOutputStream zipOut, String parentPath) throws IOException {
File[] files = dir.listFiles();//获得目录下的所有文件(包括目录和文件)
byte[] buffer = new byte[BUFFER_SIZE];//缓存大小
if (files.length == 0) {//如果目录为空另行创建
zipOut.putNextEntry(new ZipEntry(handlerFilePath(dir.toString(),parentPath)+File.separator));
zipOut.closeEntry();
} else {//如果目录下不为空 则分别处理目录和文件
for (File file : files) {
if (file.isDirectory()) {//目录情况递归遍历
doZipHandlerDir(file, zipOut, parentPath);
} else {//文件情况读文件 并写入到zip包中
doZipWriteFile(file, zipOut, parentPath, buffer);
}
}
}
}
/**
* 向zip包中写入文件
* @param file 文件对象
* @param zipOut zip输出流
* @param parentPath 父目录路径
* @param buffer 缓存
* @throws Exception 向上抛出异常
*/
private static void doZipWriteFile(File file, ZipOutputStream zipOut, String parentPath, byte[] buffer) throws IOException {
FileInputStream fis = new FileInputStream(file);
zipOut.putNextEntry(new ZipEntry(handlerFilePath(file.toString(), parentPath)));
int length = 0;//读取字节长度
while ((length = fis.read(buffer)) > 0) {
zipOut.write(buffer, 0, length);
}
zipOut.closeEntry();
fis.close();
}
/**
* 处理路径 将绝对路径处理成相对路径 否则zip包中会出现绝对路径下的每一层目录
* @param realPath 绝度路径
* @param parentPath 需要去掉的父路径
* @return 处理后的相对路径
* @throws IOException
* @throws Exception 找不到父路径时抛出异常
*/
private static String handlerFilePath(String realPath, String parentPath) throws IOException {
int index = -1;
index = realPath.indexOf(parentPath);
if (index == -1) {
throw new IOException("路径错误");
}
return realPath.substring(index + parentPath.length());
}

打出来的zip android手机无法解压,请各位支招
...全文
1140 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复
源码下载地址: https://pan.quark.cn/s/8d2c461c797c JavaWeb程序设计构成了掌握Web交互式应用程序开发的核心领域,对于初学者来说,精通这一技术具有决定性意义。在“JavaWeb程序设计(第三版)作业答案”中,我们可以预期获得针对该教材习题的一系列深入解析,从而协助学习者强化知识体系。 JavaWeb所含的技术组件涵盖了Servlet、JSP(JavaServer Pages)、JDBC(Java Database Connectivity)以及各类框架如Spring MVC、Struts等。Servlet是Java平台提供的一种扩展服务器功能的接口,能够处理HTTP请求并生成相应的反馈。JSP则是一种用于构建动态网页的工具,它支持开发者将HTML代码与Java代码进行整合编写,从而简化了Web应用程序的开发流程。 作业答案通常会涉及以下几个核心内容: 1. **Servlet基础**:可能含Servlet生命周期、init(), service(), destroy()方法的应用,以及如何在web.xml文件中设定Servlet的映射关系。 2. **JSP基础**:JSP的九大内置对象,如request、response、session、application等的使用,以及EL(Expression Language)和JSTL(JavaServer Pages Standard Tag Library)的实际操作。 3. **HTTP协议理解**:GET和POST请求方法的差异,请求头与响应头的应用,以及会话管理的概念阐释。 4. **JDBC数据库操作**:与数据库建立连接,执行SQL指令,处理查询结果集,以及...
源码链接: https://pan.quark.cn/s/a4b39357ea24 斐讯K2是一款广受用户青睐的无线路由器,其运行表现稳定且具备较高的可操作性,在DIY爱好者群体中拥有极高的声誉。本资料将系统性地阐述斐讯K2的固件刷机方法及其关联的技术要点。固件升级是路由器爱好者改善设备性能、扩展功能的一种普遍手段,经由替换出厂固件,能够达成更加个性化的网络配置、增强安全防护等目标。斐讯K2固件资源库涵盖了多种知名的非官方固件,诸如Tomato Pheonix 不死鸟、高恪、PandoraBox 潘多拉等,这些固件均具备独特的优势,能够适配不同用户的需求。 1. Tomato Pheonix 不死鸟:Tomato是一款立足于Linux的开源固件,以其精巧、高效而备受推崇。不死鸟版本是专门为华硕及斐讯路由器优化的分支,提供了卓越的QoS(服务质量)配置、详尽的图表监控以及便捷的固件升级途径。对于那些需要精准调控带宽和监测网络状态的用户而言,这是一个理想的选项。 2. 高恪:高恪固件是OpenWrt的定制化版本,着重于操作的便捷性和运行的可靠性,特别适合对路由器操作不甚熟悉的用户群体。它提供了一些实用的功能,例如内置的广告屏蔽、快速测速工具等,同时保留了OpenWrt的适应性。 3. PandoraBox 潘多拉:潘多拉盒是另一款基于OpenWrt的固件,它以丰富的插件库和强大的自定义潜力而闻名。用户能够依据个人需求安装各类插件,实现更多功能,如远程接入、DDNS(动态域名解析服务)等。 4. 官方固件的纯净版本与定制版本:官方固件通常更侧重于稳定性,纯净版意味着未预置额外的应用或服务,适合注重稳定性的用户。定制版则可能含了制造商的特色功能或优...

80,489

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧