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 ====================== ...
随机推荐
- 利用Ajax实现前端与.net后端实现数据交互
使用场景和需求:用户在地址栏输入请求地址,先.net服务器发送页面请求,该页面包含Echart图表,在页面中向.net后端发送数据请求,获取数据后,将数据填充到Echart图表中.其中包含带参与不带参 ...
- Android Binder机制原理(史上最强理解,没有之一)(转)
原文地址: http://blog.csdn.net/universus/article/details/6211589 Binder是Android系统进程间通信(IPC)方式之一.Linux已经拥 ...
- JavaScript(7)——事件2.0
事件 [事件类型] 不同的事件类型具有不同的信息. [UI事件] 指的是那些不一定与用户操作有关的事件.当用户与页面上的元素交互时触发. load事件:当页面完全加载后,就会 触发window 上面的 ...
- 查看Linux系统文本编码-方便修改ssh编码一致
首先,Linux系统发行的时候全世界都一样,系统是中文的还是英文的完全取决于你选择的语言包.不同国家的人在安装使用的时候选择属于自己国家的语言包,应用程序中的语言也不是写死的,它根据系统的设置来调用相 ...
- Android中的Fragment页面切换和selector选择器
效果如图: 提示:下面是用的整个的图片 下面看代码: //--------------------这是主页面布局文件----------------------- <?xml version=& ...
- Tenured 区并发垃圾回收器CMS介绍
当使用CMS收集器时,当开始进行收集时,old代的收集过程如下所示:1,首先jvm根据-XX:CMSInitiatingOccupancyFraction,-XX:+UseCMSInitiatingO ...
- jquery-图片轮播(新手请大神指教一下)
这是我刚学jquery写的,感觉效果不是很好. #scrollPics{ height: 330px; width: 980px; margin-bottom: 10px; overflow: hid ...
- VBS基础篇 - VBScript过程
VBS基础篇 - VBScript过程 在 VBScript 中,过程被分为两类:Sub 过程和 Function 过程. Sub过程 Sub 过程是包含在 Sub 和 End Sub 语句之间的 ...
- bash color
紫色:300A24 黄色:C4A000 Tango 紫色: 200213
- js 条件判断放大字体
<html> <head> <meta charset="utf-8" /> <title></title> <s ...