Last active
April 26, 2019 01:39
-
-
Save evilwk/468dcc3be978093f44f2ded7588d9029 to your computer and use it in GitHub Desktop.
Zip文件解压 #Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mcelf.TutorialMap.util; | |
import java.io.*; | |
import java.util.zip.ZipEntry; | |
import java.util.zip.ZipInputStream; | |
/** | |
* @author evilwk <evilwk@gmail.com> on 2016/8/6 0006 | |
*/ | |
@SuppressWarnings("ResultOfMethodCallIgnored") | |
public class ZipUtil { | |
public static final String EXT = ".zip"; | |
private static final String BASE_DIR = ""; | |
private static final String PATH = File.separator; | |
private static final int BUFFER = 1024; | |
public static void decompress(String srcPath) throws Exception { | |
File srcFile = new File(srcPath); | |
decompress(srcFile); | |
} | |
public static void decompress(File srcFile) throws Exception { | |
String basePath = srcFile.getParent(); | |
decompress(srcFile, basePath); | |
} | |
public static void decompress(String srcPath, String destPath) | |
throws Exception { | |
File srcFile = new File(srcPath); | |
decompress(srcFile, destPath); | |
} | |
public static void decompress(File srcFile, String destPath) | |
throws Exception { | |
decompress(srcFile, new File(destPath)); | |
} | |
public static void decompress(File srcFile, File destFile) throws Exception { | |
ZipInputStream zis = new ZipInputStream(new FileInputStream(srcFile)); | |
decompress(destFile, zis); | |
zis.close(); | |
} | |
public static void decompress(InputStream inputStream, File destFile) throws Exception { | |
ZipInputStream zis = new ZipInputStream(inputStream); | |
decompress(destFile, zis); | |
zis.close(); | |
} | |
private static void decompress(File destFile, ZipInputStream zis) | |
throws Exception { | |
ZipEntry entry; | |
while ((entry = zis.getNextEntry()) != null) { | |
String dir = destFile.getPath() + File.separator + entry.getName(); | |
File dirFile = new File(dir); | |
if (entry.isDirectory()) { | |
dirFile.mkdirs(); | |
} else { | |
decompressFile(dirFile, zis); | |
} | |
zis.closeEntry(); | |
} | |
} | |
private static void decompressFile(File destFile, ZipInputStream zis) | |
throws Exception { | |
BufferedOutputStream outputStream = new BufferedOutputStream( | |
new FileOutputStream(destFile)); | |
int readCount; | |
byte data[] = new byte[BUFFER]; | |
while ((readCount = zis.read(data, 0, BUFFER)) != -1) { | |
outputStream.write(data, 0, readCount); | |
} | |
outputStream.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment