android zip解压缩
生活随笔
收集整理的這篇文章主要介紹了
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
Exception:bad byte at 4。我找了好久最后發現,其實就是文件名不能是漢字。因為我的zip包里有帶漢字的文件。這樣Android就不夠解壓出現異常。Android解壓的zip包不處理,里的東西不能是以漢字命名的。轉載于:https://www.cnblogs.com/wikiki/p/5050351.html
總結
以上是生活随笔為你收集整理的android zip解压缩的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: No architectures to
- 下一篇: CentOS7 设置开机直接进入命令行界