在android中从zip解压缩文件夹(Extract folders from zip in android)

编程入门 行业动态 更新时间:2024-10-08 03:37:46
在android中从zip解压缩文件夹(Extract folders from zip in android)

我的应用程序必须下载一个zip,并且必须在应用程序文件夹中解压缩它。 问题是zip没有文件而是文件夹,每个文件夹中都有不同的文件。 我会保持相同的结构,但我不知道它是怎么做的。 如果我用一个文件的拉链但没有文件夹的拉链,我成功了。 有人知道它是怎么做到的吗? 非常感谢。

My application has to download a zip and has to unzip it in the application folder. The problem is that the zip doesn't have files but folders and in each folder there are different files. I would to keep the same structure but I don't know how do it. I succeed if i do it with a zip of files but not with a zip of folders. There is somebody who knows how do it? Many thanks.

最满意答案

您需要为ZIP存档中的每个目录条目创建目录。 这是我编写和使用的方法,它将保留目录结构:

/** * Unzip a ZIP file, keeping the directory structure. * * @param zipFile * A valid ZIP file. * @param destinationDir * The destination directory. It will be created if it doesn't exist. * @return {@code true} if the ZIP file was successfully decompressed. */ public static boolean unzip(File zipFile, File destinationDir) { ZipFile zip = null; try { destinationDir.mkdirs(); zip = new ZipFile(zipFile); Enumeration<? extends ZipEntry> zipFileEntries = zip.entries(); while (zipFileEntries.hasMoreElements()) { ZipEntry entry = zipFileEntries.nextElement(); String entryName = entry.getName(); File destFile = new File(destinationDir, entryName); File destinationParent = destFile.getParentFile(); if (destinationParent != null && !destinationParent.exists()) { destinationParent.mkdirs(); } if (!entry.isDirectory()) { BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry)); int currentByte; byte data[] = new byte[DEFUALT_BUFFER]; FileOutputStream fos = new FileOutputStream(destFile); BufferedOutputStream dest = new BufferedOutputStream(fos, DEFUALT_BUFFER); while ((currentByte = is.read(data, 0, DEFUALT_BUFFER)) != EOF) { dest.write(data, 0, currentByte); } dest.flush(); dest.close(); is.close(); } } } catch (Exception e) { return false; } finally { if (zip != null) { try { zip.close(); } catch (IOException ignored) { } } } return true; }

You will need to create the directories for each directory entry in the ZIP archive. Here is a method I wrote and use that will keep the directory structure:

/** * Unzip a ZIP file, keeping the directory structure. * * @param zipFile * A valid ZIP file. * @param destinationDir * The destination directory. It will be created if it doesn't exist. * @return {@code true} if the ZIP file was successfully decompressed. */ public static boolean unzip(File zipFile, File destinationDir) { ZipFile zip = null; try { destinationDir.mkdirs(); zip = new ZipFile(zipFile); Enumeration<? extends ZipEntry> zipFileEntries = zip.entries(); while (zipFileEntries.hasMoreElements()) { ZipEntry entry = zipFileEntries.nextElement(); String entryName = entry.getName(); File destFile = new File(destinationDir, entryName); File destinationParent = destFile.getParentFile(); if (destinationParent != null && !destinationParent.exists()) { destinationParent.mkdirs(); } if (!entry.isDirectory()) { BufferedInputStream is = new BufferedInputStream(zip.getInputStream(entry)); int currentByte; byte data[] = new byte[DEFUALT_BUFFER]; FileOutputStream fos = new FileOutputStream(destFile); BufferedOutputStream dest = new BufferedOutputStream(fos, DEFUALT_BUFFER); while ((currentByte = is.read(data, 0, DEFUALT_BUFFER)) != EOF) { dest.write(data, 0, currentByte); } dest.flush(); dest.close(); is.close(); } } } catch (Exception e) { return false; } finally { if (zip != null) { try { zip.close(); } catch (IOException ignored) { } } } return true; }

更多推荐

本文发布于:2023-08-07 16:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465273.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:解压缩   文件夹   zip   android   Extract

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!