[Java]解压缩zip文件示例代码

2021-07-02 1185点热度 0人点赞 0条评论

前面文章介绍了如何使用Java压缩文件和文件夹:https://blog.terrynow.com/2021/07/01/java-zip-files-how-to/

本篇Java示例代码是如何解压缩文件:

public static void main(String[] args) throws Exception {
unzipFile(new File("/Users/Terry/Downloads/test.zip"), new File("/Users/Terry/Downloads/test"));
}
/**
* @param zipFile 需要解压的zip文件
* @param baseDir 解压到的目标文件夹
*/
private static void unzipFile(File zipFile, File baseDir) throws Exception {
ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
ZipEntry zipEntry = zis.getNextEntry();
byte[] buffer = new byte[4096];
while (zipEntry != null) {
if (zipEntry.getName().contains(".DS_Store")) {//我的电脑有.DS_Store,忽略
zipEntry = zis.getNextEntry();
continue;
}
if (zipEntry.isDirectory()) {//如果是文件夹,就新建文件夹
File dir = new File(baseDir, zipEntry.getName());
dir.mkdirs();
} else {//如果是文件,就解压出来,fileName会包含文件夹的相对路径的,所以如果有多层,就会保存到相应的文件夹下的
String fileName = zipEntry.getName();
File newFile = new File(baseDir, fileName);
System.out.println(fileName);
FileOutputStream fos = new FileOutputStream(newFile);
int len;
while ((len = zis.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
fos.close();
}
zis.closeEntry();
zipEntry = zis.getNextEntry();
}
zis.closeEntry();
zis.close();
}
public static void main(String[] args) throws Exception { unzipFile(new File("/Users/Terry/Downloads/test.zip"), new File("/Users/Terry/Downloads/test")); } /** * @param zipFile 需要解压的zip文件 * @param baseDir 解压到的目标文件夹 */ private static void unzipFile(File zipFile, File baseDir) throws Exception { ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile)); ZipEntry zipEntry = zis.getNextEntry(); byte[] buffer = new byte[4096]; while (zipEntry != null) { if (zipEntry.getName().contains(".DS_Store")) {//我的电脑有.DS_Store,忽略 zipEntry = zis.getNextEntry(); continue; } if (zipEntry.isDirectory()) {//如果是文件夹,就新建文件夹 File dir = new File(baseDir, zipEntry.getName()); dir.mkdirs(); } else {//如果是文件,就解压出来,fileName会包含文件夹的相对路径的,所以如果有多层,就会保存到相应的文件夹下的 String fileName = zipEntry.getName(); File newFile = new File(baseDir, fileName); System.out.println(fileName); FileOutputStream fos = new FileOutputStream(newFile); int len; while ((len = zis.read(buffer)) > 0) { fos.write(buffer, 0, len); } fos.close(); } zis.closeEntry(); zipEntry = zis.getNextEntry(); } zis.closeEntry(); zis.close(); }
public static void main(String[] args) throws Exception {
    unzipFile(new File("/Users/Terry/Downloads/test.zip"), new File("/Users/Terry/Downloads/test"));
}

/**
 * @param zipFile 需要解压的zip文件
 * @param baseDir 解压到的目标文件夹
 */
private static void unzipFile(File zipFile, File baseDir) throws Exception {
    ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
    ZipEntry zipEntry = zis.getNextEntry();
    byte[] buffer = new byte[4096];
    while (zipEntry != null) {
        if (zipEntry.getName().contains(".DS_Store")) {//我的电脑有.DS_Store,忽略
            zipEntry = zis.getNextEntry();
            continue;
        }
        if (zipEntry.isDirectory()) {//如果是文件夹,就新建文件夹
            File dir = new File(baseDir, zipEntry.getName());
            dir.mkdirs();
        } else {//如果是文件,就解压出来,fileName会包含文件夹的相对路径的,所以如果有多层,就会保存到相应的文件夹下的
            String fileName = zipEntry.getName();
            File newFile = new File(baseDir, fileName);
            System.out.println(fileName);
            FileOutputStream fos = new FileOutputStream(newFile);
            int len;
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }
            fos.close();
        }
        zis.closeEntry();
        zipEntry = zis.getNextEntry();
    }
    zis.closeEntry();
    zis.close();
}

 

admin

这个人很懒,什么都没留下

文章评论

您需要 登录 之后才可以评论