android zip解压缩
2015-12-16 10:36
375 查看
android zip解压缩
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包不处理,里的东西不能是以汉字命名的。
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包不处理,里的东西不能是以汉字命名的。
相关文章推荐
- 短信(一)
- dialog中出现异常android.view.WindowManager$BadTokenException
- android.content.res.Resources$NotFoundException: Unable to find resource ID #0xffffffff
- android dialog 里面获取Activity的Context
- 详解Android中Handler的内部实现原理
- android源码分析(ZZ)
- Android摄像头 怎么搞?
- Android TabHost取消默认加载第一个tab的问题
- 详解Android中Handler的使用方法
- Android 防止设置大字体导致布局混乱
- Android动画Property Animation入门
- Android通过startService播放背景音乐
- android 输入法弹出关闭与禁用
- Android植物大战僵尸小游戏
- Android通过startService实现文件批量下载
- android TextView中间删除线显示
- Android 强引用、软引用、弱引用、虚引用
- Android中bindService基本使用方法概述
- android上开源的酷炫的交互动画和视觉效果
- Android实战 - 音心播发器 (MusicService ,Notification, MainActivity 总结)