public class ZipUtil {
public static void main(String[] args) throws Exception {
// String[] ss = new String[2];
// ss[0] = "D:\\adManageUpload\\excel\\1552116277671_.xlsx";
// ss[1] = "D:\\adManageUpload\\excel\\1552120508265_.xlsx";
// zip("C:/Users/Administrator/Desktop/111.zip", ss);
// zip("C:\\Users\\Administrator\\Desktop\\temp.zip", "C:\\Users\\Administrator\\Desktop\\temp");
}
/**
* @param zipName
* 输出的压缩文件名称
* @param pathList
* 文件路径列表
*/
public static void zip(String zipName, String[] pathList) {
ZipOutputStream out = null;
BufferedOutputStream bos = null;
try {
File file = new File(zipName);
out = new ZipOutputStream(new FileOutputStream(zipName), StandardCharsets.UTF_8);// 创建zip输出流
bos = new BufferedOutputStream(out);// 创建缓冲输出流
compress(out, bos, file.getName(), pathList);// 调用函数
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != bos)
bos.close();
if (null != out)
out.close();
} catch (IOException e) {
}
}
}
/**
* @param out
* 输出流
* @param bos
* 缓冲流
* @param sourceFile
* 压缩源文件
* @param base
* 当前被压缩文件的文件名,形成压缩文件时候的目录层级
* @throws Exception
*/
private static void compress(ZipOutputStream out, BufferedOutputStream bos, String zipName, String[] pathList) throws Exception {
out.putNextEntry(new ZipEntry(zipName + File.separator));
Set<String> set = new HashSet<String>();
for (String string : pathList) {
if (set.contains(string)) {
continue;
}
File f = new File(string);
out.putNextEntry(new ZipEntry(zipName + File.separator + f.getName()));
FileInputStream fos = new FileInputStream(f);
byte[] bs = new byte[1024];
// 将源文件写入到zip文件中
while (fos.read(bs) != -1) {
bos.write(bs);
bos.flush();
}
fos.close();
set.add(string);
}
}
/**
* @param zipName
* 输出的压缩文件名称
* @param sourceName
* 输入的文件或者文件夹
*/
public static void zip(String zipName, String sourceName) {
ZipOutputStream out = null;
BufferedOutputStream bos = null;
try {
out = new ZipOutputStream(new FileOutputStream(zipName), StandardCharsets.UTF_8);// 创建zip输出流
bos = new BufferedOutputStream(out);// 创建缓冲输出流
File sourceFile = new File(sourceName);
compress(out, bos, sourceFile, sourceFile.getName());// 调用函数
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != bos)
bos.close();
if (null != out)
out.close();
} catch (IOException e) {
}
}
}
/**
* @param out
* 输出流
* @param bos
* 缓冲流
* @param sourceFile
* 压缩源文件
* @param base
* 当前被压缩文件的文件名,形成压缩文件时候的目录层级
* @throws Exception
*/
private static void compress(ZipOutputStream out, BufferedOutputStream bos, File sourceFile, String base) throws Exception {
// 如果路径为目录(文件夹)
if (sourceFile.isDirectory()) {
// 取出文件夹中的文件(或子文件夹)
File[] flist = sourceFile.listFiles();
if (flist.length == 0) {// 如果文件夹为空,则只需在目的地zip文件中写入一个目录进入点
out.putNextEntry(new ZipEntry(base + File.separator));
} else {// 如果文件夹不为空,则递归调用compress,文件夹中的每一个文件(或文件夹)进行压缩
for (int i = 0; i < flist.length; i++) {
compress(out, bos, flist[i], base + File.separator + flist[i].getName());
}
}
} else {// 如果不是目录(文件夹),即为文件,则先写入目录进入点,之后将文件写入zip文件中
out.putNextEntry(new ZipEntry(base));
FileInputStream fos = new FileInputStream(sourceFile);
byte[] bs = new byte[1024];
// 将源文件写入到zip文件中
while (fos.read(bs) != -1) {
bos.write(bs);
bos.flush();
}
fos.close();
}
}
// /***
// * 压缩GZip
// *
// * @param data
// * @return
// */
// public static byte[] gZip(byte[] data) {
// byte[] b = null;
// try {
// ByteArrayOutputStream bos = new ByteArrayOutputStream();
// GZIPOutputStream gzip = new GZIPOutputStream(bos);
// gzip.write(data);
// gzip.finish();
// gzip.close();
// b = bos.toByteArray();
// bos.close();
// } catch (Exception ex) {
// ex.printStackTrace();
// }
// return b;
// }
//
// /***
// * 解压GZip
// *
// * @param data
// * @return
// */
// public static byte[] unGZip(byte[] data) {
// byte[] b = null;
// try {
// ByteArrayInputStream bis = new ByteArrayInputStream(data);
// GZIPInputStream gzip = new GZIPInputStream(bis);
// byte[] buf = new byte[1024];
// int num = -1;
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
// while ((num = gzip.read(buf, 0, buf.length)) != -1) {
// baos.write(buf, 0, num);
// }
// b = baos.toByteArray();
// baos.flush();
// baos.close();
// gzip.close();
// bis.close();
// } catch (Exception ex) {
// ex.printStackTrace();
// }
// return b;
// }
//
// /***
// * 解压GZip
// *
// * @param data
// * @return
// */
// public static byte[] unZip(byte[] data) {
// byte[] b = null;
// try {
// ByteArrayInputStream bis = new ByteArrayInputStream(data);
// ZipInputStream gzip = new ZipInputStream(bis);
// byte[] buf = new byte[data.length];
// int num = -1;
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
// while ((num = gzip.read(buf, 0, buf.length)) != -1) {
// baos.write(buf, 0, num);
// }
// b = baos.toByteArray();
// baos.flush();
// baos.close();
// gzip.close();
// bis.close();
// } catch (Exception ex) {
// ex.printStackTrace();
// }
// return b;
// }
}
package com.xj.hhjk.common.util;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashSet;
import java.util.Set;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)