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手机无法解压,请各位支招
...全文
1097 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

80,350

社区成员

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

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