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 widgetszName = 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 fileFileOutputStream out = new FileOutputStream(file);int len;byte[] buffer = new byte[1024];// read (len) bytes into bufferwhile ((len = inZip.read(buffer)) != -1) {// write (len) byte from buffer at the position 0out.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 widgetszName = 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 fileFileOutputStream out = new FileOutputStream(file);int len;byte[] buffer = new byte[1024];// read (len) bytes into bufferwhile ((len = inZip.read(buffer)) != -1) {// write (len) byte from buffer at the position 0out.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 ZIPZipOutputStream outZip = new ZipOutputStream(new FileOutputStream(zipFileString));// create the fileFile file = new File(srcFileString);// compressZipFiles(file.getParent() + File.separator, file.getName(), outZip);// finish and closeoutZip.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 {// folderString fileList[] = file.list();// no child file and compressif (fileList.length <= 0) {ZipEntry zipEntry = new ZipEntry(fileString + File.separator);zipOutputSteam.putNextEntry(zipEntry);zipOutputSteam.closeEntry();}// child files and recursionfor (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 widgetszName = 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
转载于:https://www.cnblogs.com/wikiki/p/5050351.html
android zip解压缩相关推荐
- Android Zip解压缩类处理中文名乱码
写在前面: 最近在做epub格式的解析, 采用的是java.util.zipFile类,发现这个实体类 对中文支持不是很好.换了Ant包.结果编译体积就大了很多. 目前正在想办法解决. 目前Andro ...
- android zip解压缩(含有子目录)
为什么80%的码农都做不了架构师?>>> note: 如果目录中含有中文名称, 要用substr = new String(substr.getBytes("8859 ...
- java 压缩技术_Java压缩技术(三) ZIP解压缩——Java原生实现
JavaEye的朋友跟我说:"你一口气把ZIP压缩和解压缩都写到一个帖子里,我看起来很累,不如分开好阅读".ok,面向读者需求,我做调整,这里单说ZIP解压缩! 相关链接: Jav ...
- Java压缩技术(三) ZIP解压缩——Java原生实现
转载自 Java压缩技术(三) ZIP解压缩--Java原生实现 解压缩与压缩运作方式相反,原理大抵相同,由ZipInputStream通过read方法对数据解压,同时需要通过CheckedInp ...
- Mac 解压zip文件错误:无法将*.zip解压缩到 (错误 1-操作不被允许)
错误提示: 无法将"*.zip"解压缩到"" (错误 1-操作不被允许)或者 解压缩失败 英文提示: "Unable to unarchive int ...
- mac无法将xxx.zip解压缩到下载(错误 - 无此文件或目录)
文件过大时,mac 无法将xxx.zip解压缩到下载(错误 - 无此文件或目录) 此时可以修改zip后缀为rar, 使用Unarchiver解压.
- Android Zip文件解压缩代码
在Android平台中如何实现Zip文件的解压缩功能呢? 因为Android内部已经集成了zlib库,对于英文和非密码的Zip文件解压缩还是比较简单的,下面Android123给大家一个解压缩zip的 ...
- android zip解压出错,Android:解压缩文件会引发数据错误或CRC错误
我正在开发一个下载zip文件并在本地解压缩的项目.我遇到的问题是解压缩过程在5%的时间内起作用. 在这一点上,这对我来说是一个谜,因为有时它可以工作,但大多数时候它会抛出数据或crc错误.即使zip文 ...
- Android zip文件解压缩工具类
今天项目提了一个新需求:把html网页放在本地,如果后台修改了网页,手机端要去后台下载压缩包并解压后把本地的网页跟新成最新的,请求后台接口,根据返回的结果中的某个字段判断是否需要下载zip文件,如果需 ...
最新文章
- JAVA方法 字符串与unicode的相互转换
- 深度学习会不会被取代?深度学习必看发展史
- activiti 多租户_Activiti中具有独立数据库架构的多租户
- 在Spring MVC中处理域对象
- Nsis 使用1-- 依条件显示自定义页面 custom page on condition
- for循环两个分号之间不要乱加判断条件(记洛谷P2141题WA的经历,Java语言描述)
- 利用python随机生成姓名的实例教程
- Java【第九篇】异常处理
- Java实现常见的排序算法
- CFree 5.0最新注册码
- mongodb3 重启_“打工人”必备技能 OPPOR9splus重启让手机再战一年|手机|打工人|oppor|splus...
- linux使用can卡
- PyTorch-1.10(十三)--torch.optim基本用法
- LNK1104:无法打开文件 C:\PhoneYou\roshan-0.6.5\lib\roshan\plugins\Goblin\teacher\teacher.dll
- Sublime Text3 配置LaTex编辑器
- H3C RRPP实验
- webservice的安全问题
- 谷歌标签恢复_避免/从Google惩罚中恢复
- Github星标90K?京东架构师一篇讲明白百亿级并发系统架构设计
- Java基础之购物清单