android zip解压缩
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
android zip解压缩的更多相关文章
- Android中的Zip解压缩
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...
- Mac 解压zip文件错误:无法将"*.zip"解压缩到"" (错误 1-操作不被允许)
错误提示: 无法将"*.zip"解压缩到"" (错误 1-操作不被允许)或者 解压缩失败 英文提示: "Unable to unarchive int ...
- ZIP解压缩工具类
import java.io.File; import org.apache.tools.ant.Project; import org.apache.tools.ant.taskdefs.Expan ...
- ZIP解压缩文件的工具类【支持多级文件夹|全】
ZIP解压缩文件的工具类[支持多级文件夹|全] 作者:Vashon 网上有非常多的加压缩演示样例代码.可是都仅仅是支持一级文件夹的操作.假设存在多级文件夹的话就不行了. 本解压缩工具类经过多次检查及重 ...
- ZIP解压缩文件的工具类【支持多级目录|全】
ZIP解压缩文件的工具类[支持多级目录|全] 作者:Vashon 网上有很多的加压缩示例代码,但是都只是支持一级目录的操作,如果存在多级目录的话就不行了.本解压缩工具类经过多次检查及重构,最终分享给大 ...
- Java压缩技术(三) ZIP解压缩——Java原生实现
原文:http://snowolf.iteye.com/blog/642492 JavaEye的朋友跟我说:“你一口气把ZIP压缩和解压缩都写到一个帖子里,我看起来很累,不如分开好阅读”.ok,面向读 ...
- Android Zip文件解压缩代码
2011-04-01 17:58:52| 分类: Android |举报 |字号 订阅 在Android平台中如何实现Zip文件的解压 缩功能呢? 因为Android内部已经集成了zlib库,对 ...
- Android zip文件压缩解压缩
DirTraversal.java <P style="TEXT-ALIGN: left; PADDING-BOTTOM: 0px; WIDOWS: 2; TEXT-TRANSFORM ...
- Linux下的压缩zip,解压缩unzip命令详解及实例
实例:压缩服务器上当前目录的内容为xxx.zip文件 zip -r xxx.zip ./* 解压zip文件到当前目录 unzip filename.zip ====================== ...
随机推荐
- sha加密算法
密钥生成 公钥(e,n) 私钥(d,n) 找两个互质的大素数p和q, 计算n=p*p, f(n)=(p-1)*(q-1) 选择随机整数e(e和f(n)互质) de=f(n)mod 1 利用公钥加密 ...
- hdu 5493 Queue treap实现将元素快速插入到第i个位置
input T 1<=T<=1000 n 1<=n<=100000 h1 k1 h2 k2 ... ... hn kn 1<=hi<=1e9 0<=ki&l ...
- msdn我告诉你
http://msdn.itellyou.cn/ 微软旗下所有的msdn订阅软件资源 均为ed2k资源 Business Solutions MSDN Library 工具和资源 应用程序 开发人员工 ...
- MFC中为菜单或按钮添加快捷键功能
1.新建一快捷键资源,ACCELERATOR,关联相应的ID号,下图所示中,其中,第一个ID为自定义快捷键ID,按CTRL+R,此时响应该ID以应的消息响应函数, 第二个ID为菜单ID,此时按CTRL ...
- where条件的lambda转化为sql语句
网上找的源码,但是博主说有bug 让自己调试.这个是我经过多次修改后的代码,可以直接用 public static class LambdaToSqlHelper { #region 基础方法 #re ...
- td里的内容宽度自适应 及 鼠标放上显示标题div title
td里的内容自适应宽度, 用 width:100%控制 strRight+="<td bordercolor='#DEDEDE' width='500px' height='50px' ...
- android--屏幕旋转方法总结
在介绍之前,我们需要先了解默认情况下android屏幕旋转的机制: 默认情况下,当用户手机的重力感应器打开后,旋转屏幕方向,会导致当前activity发生onDestroy-> onCreate ...
- openCV(三)---图像缩放
UIImage *img1 = [UIImage imageNamed:@"1448941176867"]; //将UIImage转换为IplImage格式 IplImage *p ...
- C# 垃圾回收机制(转)
摘要:今天我们漫谈C#中的垃圾回收机制,本文将从垃圾回收机制的原理讲起,希望对大家有所帮助. GC的前世与今生 虽然本文是以.NET作为目标来讲述GC,但是GC的概念并非才诞生不久.早在1958年,由 ...
- sqlserver日志文件过大的处理方法
SqlServer 数据库中与备份和恢复相关的日志文件有MDF和LDF文件 存在于\\sqlserver_install_dir\MSSQL\Data\1.文件解释.mdf文件 MDF是SQ ...