feat: 添加GZIP压缩和ZIP压缩

Signed-off-by: 502647092 <admin@yumc.pw>
merge/1/MERGE
502647092 2017-02-18 11:17:33 +08:00
parent 09eda70c2d
commit 104c35a0cf
1 changed files with 138 additions and 2 deletions

View File

@ -1,13 +1,21 @@
package pw.yumc.YumCore.kit;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.util.Enumeration;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
/**
* ZIP
@ -29,7 +37,7 @@ public class ZipKit {
/**
* ZIP
*
*
* @param zipFile
* zip
* @param destPath
@ -45,7 +53,7 @@ public class ZipKit {
/**
* ZIP
*
*
* @param zipFile
* zip
* @param destPath
@ -71,4 +79,132 @@ public class ZipKit {
}
zipObj.close();
}
/**
* ZIP
*
* @param inputFile
*
* @param zipFileName
*
* @throws IOException
* IO
*/
public static void zip(File inputFile, String zipFileName) throws IOException {
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFileName));
BufferedOutputStream bos = new BufferedOutputStream(zos);
zip(zos, inputFile, inputFile.getName(), bos);
bos.close();
zos.close(); // 输出流关闭
}
/**
* ZIP
*
* @param zos
* Zip
* @param file
*
* @param base
*
* @param bos
*
* @throws IOException
* IO
*/
private static void zip(ZipOutputStream zos, File file, String base, BufferedOutputStream bos) throws IOException { // 方法重载
if (file.isDirectory()) {
File[] fl = file.listFiles();
if (fl == null || fl.length == 0) {
zos.putNextEntry(new ZipEntry(base + "/")); // 创建zip压缩进入点base
} else {
for (File fl1 : fl) {
zip(zos, fl1, base + "/" + fl1.getName(), bos); // 递归遍历子文件夹
}
}
} else {
zos.putNextEntry(new ZipEntry(base)); // 创建zip压缩进入点base
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
int b;
while ((b = bis.read()) != -1) {
bos.write(b); // 将字节流写入当前zip目录
}
bos.flush();
bis.close();
fis.close(); // 输入流关闭
}
}
/**
* GZIP
*
* @param file
*
* @throws IOException
* IO
*/
public static void gzip(File file) throws IOException {
gzip(file, file.getName() + ".gz", true);
}
/**
* GZIP
*
* @param file
*
* @param out
*
* @throws IOException
* IO
*/
public static void gzip(File file, String out) throws IOException {
gzip(file, out, true);
}
/**
* GZIP
*
* @param file
*
* @param out
*
* @param delete
*
* @throws IOException
* IO
*/
public static void gzip(File file, String out, boolean delete) throws IOException {
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos = new FileOutputStream(out);
gzip(fis, fos);
fis.close();
fos.flush();
fos.close();
if (delete) {
file.delete();
}
}
/**
* GZIP
*
* @param is
*
* @param os
*
* @throws IOException
* IO
*/
public static void gzip(InputStream is, OutputStream os) throws IOException {
GZIPOutputStream gos = new GZIPOutputStream(os);
int count;
byte data[] = new byte[1024];
while ((count = is.read(data, 0, 1024)) != -1) {
gos.write(data, 0, count);
}
gos.finish();
gos.flush();
gos.close();
}
}