android  zip解压缩

public class ZipUtils {
public ZipUtils() { }

     /*
      以输入流的形式解压
    */
public static void UnZipFolder(InputStream zipFileString,
String outPathString) throws Exception {
ZipInputStream inZip = new ZipInputStream(zipFileString);
ZipEntry zipEntry;
String szName = "";
while ((zipEntry = inZip.getNextEntry()) != null) {
szName = zipEntry.getName();
if (zipEntry.isDirectory()) {
// get the folder name of the widget
szName = szName.substring(0, szName.length() - 1);
File folder = new File(outPathString + File.separator + szName);
folder.mkdirs();
} else { File file = new File(outPathString + File.separator + szName);
file.createNewFile();
// get the output stream of the file
FileOutputStream out = new FileOutputStream(file);
int len;
byte[] buffer = new byte[1024];
// read (len) bytes into buffer
while ((len = inZip.read(buffer)) != -1) {
// write (len) byte from buffer at the position 0
out.write(buffer, 0, len);
out.flush();
}
out.close();
}
}
inZip.close();
} /**
* DeCompress the ZIP to the path
* 以文件形式解压
* @param zipFileString
* name of ZIP
* @param outPathString
* path to be unZIP
* @throws Exception
*/
public static void UnZipFolder(String zipFileString, String outPathString)
throws Exception {
ZipInputStream inZip = new ZipInputStream(new FileInputStream(
zipFileString));
ZipEntry zipEntry;
String szName = "";
while ((zipEntry = inZip.getNextEntry()) != null) {
szName = zipEntry.getName();
if (zipEntry.isDirectory()) {
// get the folder name of the widget
szName = szName.substring(0, szName.length() - 1);
File folder = new File(outPathString + File.separator + szName);
folder.mkdirs();
} else { File file = new File(outPathString + File.separator + szName);
file.createNewFile();
// get the output stream of the file
FileOutputStream out = new FileOutputStream(file);
int len;
byte[] buffer = new byte[1024];
// read (len) bytes into buffer
while ((len = inZip.read(buffer)) != -1) {
// write (len) byte from buffer at the position 0
out.write(buffer, 0, len);
out.flush();
}
out.close();
}
}
inZip.close();
} /**
* Compress file and folder
*
* @param srcFileString
* file or folder to be Compress
* @param zipFileString
* the path name of result ZIP
* @throws Exception
*/
public static void ZipFolder(String srcFileString, String zipFileString)
throws Exception {
// create ZIP
ZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(
zipFileString));
// create the file
File file = new File(srcFileString);
// compress
ZipFiles(file.getParent() + File.separator, file.getName(), outZip);
// finish and close
outZip.finish();
outZip.close();
} /**
* compress files
*
* @param folderString
* @param fileString
* @param zipOutputSteam
* @throws Exception
*/
private static void ZipFiles(String folderString, String fileString,
ZipOutputStream zipOutputSteam) throws Exception {
if (zipOutputSteam == null)
return;
File file = new File(folderString + fileString);
if (file.isFile()) {
ZipEntry zipEntry = new ZipEntry(fileString);
FileInputStream inputStream = new FileInputStream(file);
zipOutputSteam.putNextEntry(zipEntry);
int len;
byte[] buffer = new byte[4096];
while ((len = inputStream.read(buffer)) != -1) {
zipOutputSteam.write(buffer, 0, len);
}
zipOutputSteam.closeEntry();
} else {
// folder
String fileList[] = file.list();
// no child file and compress
if (fileList.length <= 0) {
ZipEntry zipEntry = new ZipEntry(fileString + File.separator);
zipOutputSteam.putNextEntry(zipEntry);
zipOutputSteam.closeEntry();
}
// child files and recursion
for (int i = 0; i < fileList.length; i++) {
ZipFiles(folderString, fileString + java.io.File.separator
+ fileList[i], zipOutputSteam);
}// end of for
}
} /**
* return the InputStream of file in the ZIP
*
* @param zipFileString
* name of ZIP
* @param fileString
* name of file in the ZIP
* @return InputStream
* @throws Exception
*/
public static InputStream UpZip(String zipFileString, String fileString)
throws Exception {
ZipFile zipFile = new ZipFile(zipFileString);
ZipEntry zipEntry = zipFile.getEntry(fileString);
return zipFile.getInputStream(zipEntry);
} /**
* return files list(file and folder) in the ZIP
*
* @param zipFileString
* ZIP name
* @param bContainFolder
* contain folder or not
* @param bContainFile
* contain file or not
* @return
* @throws Exception
*/
public static List<File> GetFileList(String zipFileString,
boolean bContainFolder, boolean bContainFile) throws Exception {
List<File> fileList = new ArrayList<File>();
ZipInputStream inZip = new ZipInputStream(new FileInputStream(
zipFileString));
ZipEntry zipEntry;
String szName = "";
while ((zipEntry = inZip.getNextEntry()) != null) {
szName = zipEntry.getName();
if (zipEntry.isDirectory()) {
// get the folder name of the widget
szName = szName.substring(0, szName.length() - 1);
File folder = new File(szName);
if (bContainFolder) {
fileList.add(folder);
} } else {
File file = new File(szName);
if (bContainFile) {
fileList.add(file);
}
}
}
inZip.close();
return fileList;
}
}

  

Android 解压问题(getNextEntry()抛UTFDataFormat Exception:bad byte at 0)(

java.io.UTFDataFormatException: bad byte at 12

Android zip解压网上的资料很多,但是我用时出现一个bug是getNextEntry()抛异常java.io.UTFDataFormat

Exception:bad byte at 4。我找了好久最后发现,其实就是文件名不能是汉字。因为我的zip包里有带汉字的文件。这样Android就不够解压出现异常。Android解压的zip包不处理,里的东西不能是以汉字命名的。

android zip解压缩的更多相关文章

  1. Android中的Zip解压缩

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...

  2. Mac 解压zip文件错误:无法将"*.zip"解压缩到"" (错误 1-操作不被允许)

    错误提示: 无法将"*.zip"解压缩到"" (错误 1-操作不被允许)或者 解压缩失败 英文提示: "Unable to unarchive int ...

  3. ZIP解压缩工具类

    import java.io.File; import org.apache.tools.ant.Project; import org.apache.tools.ant.taskdefs.Expan ...

  4. ZIP解压缩文件的工具类【支持多级文件夹|全】

    ZIP解压缩文件的工具类[支持多级文件夹|全] 作者:Vashon 网上有非常多的加压缩演示样例代码.可是都仅仅是支持一级文件夹的操作.假设存在多级文件夹的话就不行了. 本解压缩工具类经过多次检查及重 ...

  5. ZIP解压缩文件的工具类【支持多级目录|全】

    ZIP解压缩文件的工具类[支持多级目录|全] 作者:Vashon 网上有很多的加压缩示例代码,但是都只是支持一级目录的操作,如果存在多级目录的话就不行了.本解压缩工具类经过多次检查及重构,最终分享给大 ...

  6. Java压缩技术(三) ZIP解压缩——Java原生实现

    原文:http://snowolf.iteye.com/blog/642492 JavaEye的朋友跟我说:“你一口气把ZIP压缩和解压缩都写到一个帖子里,我看起来很累,不如分开好阅读”.ok,面向读 ...

  7. Android Zip文件解压缩代码

    2011-04-01 17:58:52|  分类: Android |举报 |字号 订阅   在Android平台中如何实现Zip文件的解压 缩功能呢? 因为Android内部已经集成了zlib库,对 ...

  8. Android zip文件压缩解压缩

    DirTraversal.java <P style="TEXT-ALIGN: left; PADDING-BOTTOM: 0px; WIDOWS: 2; TEXT-TRANSFORM ...

  9. Linux下的压缩zip,解压缩unzip命令详解及实例

    实例:压缩服务器上当前目录的内容为xxx.zip文件 zip -r xxx.zip ./* 解压zip文件到当前目录 unzip filename.zip ====================== ...

随机推荐

  1. iOS导航栏主题

    主要是取得导航栏的appearance对象,操作它就设置导航栏的主题 UINavigationBar *navBar = [UINavigationBar appearance]; 常用主题设置 导航 ...

  2. sfs - django start from scratch

    [TOC] Launch with code git spreading is obsolte lwc Installation Path D:\PythonWebSW\Django-1.5.5 ad ...

  3. 用shell获得hadoop中mapreduce任务运行结果的状态

    在近期的工作中,我需要用脚本来运行mapreduce,并且要判断运行的结果,根据结果来做下一步的动作. 开始我想到shell中获得上一条命令运行结果的方法,即判断"$?"的值 if ...

  4. Redis简介一

    Redis是一个开源的,使用C语言编写,面向“键/值”对类型数据的分布式NoSQL数据库系统,特点是高性能,持久存储,适应高并发的应用场景.Redis纯粹为应用而产生,它是一个高性能的key-valu ...

  5. 解决VS2010中winsock.h与winsock2.h冲突(重复定义)——转载

    解决VS2010中winsock.h与winsock2.h冲突(重复定义)——转载 当这两个头文件顺序颠倒时,编译会出现许多莫名其妙的错误,错误如下: 1>…\include\ws2def.h( ...

  6. MFC中修改默认启动对话框方法

    // CMyAppEApp 初始化 BOOL CMyAppEApp::InitInstance(){// 如果一个运行在 Windows XP 上的应用程序清单指定要// 使用 ComCtl32.dl ...

  7. 百度地图与融云的“冲突”(APP的.so手机架构目录,与Library的.so的手机架构目录冲突)

    在项目引进融云的IMkit时,总是报百度地图的错误,最开始以为是65535的错误,然后试着去改下百度地图,错误原因是在IMKit里面,它的.so库有这么几个目录 而我的app的libs里面的百度地图的 ...

  8. mysql 表连接

    1.子查询是指在另一个查询语句中的SELECT子句. 例句: SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2); 其中,SELECT ...

  9. 如何在sharepoint里通过correlation id查找详细的错误信息

    Sharepoint里我们经常遇到这样的错误信息: 我们能通过下面的power shell 命令来查到详细的错误信息: $correlationid = "943e6e9c-b5d9-207 ...

  10. Java-if 嵌套结构

    import java.util.Scanner; public class ifs{ public static void main(String args[]){ Scanner in=new S ...