java-example:unzipfile
Differences
This shows you the differences between two versions of the page.
java-example:unzipfile [2021/03/12 09:31] – external edit 127.0.0.1 | java-example:unzipfile [2021/08/22 13:36] (current) – morgan0329 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== 解压文件 ====== | ||
+ | <code java> | ||
+ | package com.xiaosq.example; | ||
+ | import org.apache.commons.lang3.StringUtils; | ||
+ | |||
+ | import java.io.*; | ||
+ | import java.util.Enumeration; | ||
+ | import java.util.zip.ZipEntry; | ||
+ | import java.util.zip.ZipFile; | ||
+ | |||
+ | /** | ||
+ | * @author Morgan.L | ||
+ | * @version 1.0 | ||
+ | * @date 2017/9/13 17:18 | ||
+ | */ | ||
+ | public class MainUnzipFile { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | String zipFilePath = convertFilePath(" | ||
+ | String zipFileName = " | ||
+ | String descPath = convertFilePath(" | ||
+ | |||
+ | String realDescPath = descPath + zipFileName.split(" | ||
+ | |||
+ | try { | ||
+ | unZipFiles(zipFilePath + zipFileName, | ||
+ | } catch (IOException e) { | ||
+ | e.printStackTrace(); | ||
+ | } | ||
+ | File indexHtml = new File(realDescPath + " | ||
+ | File indexJson = new File(realDescPath + " | ||
+ | if (indexHtml.exists() && indexJson.exists()) { | ||
+ | System.out.println(" | ||
+ | } else { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | @SuppressWarnings(" | ||
+ | public static void unZipFiles(String zipFile, String descPath) | ||
+ | throws IOException { | ||
+ | // | ||
+ | File pathFile = new File(descPath); | ||
+ | if (!pathFile.exists()) { | ||
+ | pathFile.mkdirs(); | ||
+ | } | ||
+ | |||
+ | // | ||
+ | ZipFile zip = new ZipFile(zipFile); | ||
+ | |||
+ | for (Enumeration entries = zip.entries(); | ||
+ | ZipEntry entry = (ZipEntry) entries.nextElement(); | ||
+ | String zipEntryName = entry.getName(); | ||
+ | InputStream in = zip.getInputStream(entry); | ||
+ | String outPath = descPath + zipEntryName; | ||
+ | |||
+ | // | ||
+ | File file = new File(outPath.substring(0, | ||
+ | if (!file.exists()) { | ||
+ | file.mkdirs(); | ||
+ | } | ||
+ | // | ||
+ | if (new File(outPath).isDirectory()) { | ||
+ | continue; | ||
+ | } | ||
+ | // | ||
+ | // | ||
+ | |||
+ | OutputStream out = new FileOutputStream(outPath); | ||
+ | byte[] buf1 = new byte[1024]; | ||
+ | int len; | ||
+ | while ((len = in.read(buf1)) > 0) { | ||
+ | out.write(buf1, | ||
+ | } | ||
+ | in.close(); | ||
+ | out.close(); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | // | ||
+ | public static String convertFilePath(String filePath) { | ||
+ | if (StringUtils.isNotBlank(filePath)) { | ||
+ | filePath = filePath.replaceAll(" | ||
+ | if (!filePath.endsWith("/" | ||
+ | filePath = filePath + "/"; | ||
+ | } | ||
+ | } | ||
+ | return filePath; | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </ |