//定义压缩包的目录 以及压缩包的名字
String zipFilePath = filePath+patrr.getTaskNo()+".zip";
/**保存网络文件*/
QunarUtil.saveURLFile(fileUrl, zipFilePath);
/**保存网络文件*/
public static void saveURLFile(String destUrl, String fileName) throws IOException {
InputStream in = new URL(destUrl).openStream();
byte[] data = IOUtils.toByteArray(in);
FileUtils.writeByteArrayToFile(new File(fileName), data);
IOUtils.closeQuietly(in);
}
log.info(QunarUtil.getStringDate()+":去哪儿全量增加-->开始解压"+filePath+patrr.getTaskNo()+".zip");
/**
* 解压zip文件
* @throws IOException
*/
public static void unzip(String srcFile, String destPath) throws IOException {
File outFile = new File(destPath);
if (!outFile.exists()) {
outFile.mkdirs();
}
org.apache.commons.compress.archivers.zip.ZipFile zipFile = new org.apache.commons.compress.archivers.zip.ZipFile(srcFile);
Enumeration<?> en = zipFile.getEntries();
ZipArchiveEntry zipEntry = null;
while (en.hasMoreElements()) {
zipEntry = (ZipArchiveEntry) en.nextElement();
if (zipEntry.isDirectory()) {
// mkdir directory
String dirName = zipEntry.getName();
dirName = dirName.substring(0, dirName.length() - 1);
File f = new File(outFile.getPath() + "/" + dirName);
f.mkdirs();
}
else {
// unzip file
File f = new File(outFile.getPath() + "/" + zipEntry.getName());
if(!f.getParentFile().exists()){
f.getParentFile().mkdirs();
}
f.createNewFile();
InputStream in = zipFile.getInputStream(zipEntry);
OutputStream out = new FileOutputStream(f);
IOUtils.copy(in, out);
out.close();
in.close();
}
}
zipFile.close();
}
//删掉ZIP
File dbFile = new File(zipFilePath);
dbFile.delete();
/**
* 压缩成zip文件
* @throws IOException
*/
public static void tozip(String srcFile, String destPath) throws IOException {
File outFile = new File(destPath);
if (!outFile.exists()) {
outFile.mkdirs();
}
//定义要压缩的文件 也就是说在D盘里有个 demo.txt 的文件(必须要有,否者会有异常,实际应用中可判断);
File file = new File(srcFile);
//定义压缩文件的名称
File zipFile = new File(destPath + "qunar.zip");
//定义输入文件流
InputStream input = new FileInputStream(file);
//定义压缩输出流
ZipOutputStream zipOut = null;
//实例化压缩输出流,并制定压缩文件的输出路径 就是D盘下,名字叫 demo.zip
zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
zipOut.putNextEntry(new ZipEntry(file.getName()));
//设置注释 zipOut.setComment("www.demo.com");
int temp = 0;
while((temp = input.read()) != -1) {
zipOut.write(temp);
}
input.close();
zipOut.close();
}
/**
* 文件打包成ZIP
*
* @param filePath
* @param zipFilePath
* @throws Exception
*/
public static void zip(String filePath, String zipFilePath)
throws Exception {
FileOutputStream dest = null;
ZipOutputStream out = null;
BufferedInputStream origin = null;
try {
dest = new FileOutputStream(zipFilePath);
out = new ZipOutputStream(new BufferedOutputStream(dest));
byte data[] = new byte[BUFFER];
File f = new File(filePath);
// File files[] = f.listFiles();
// for (int i = 0; i < files.length; i++) {
FileInputStream fi = new FileInputStream(f);
origin = new BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(f.getName());
out.putNextEntry(entry);
int count;
while ((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
// }
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != origin) {
origin.close();
}
if (null != out) {
out.close();
}
if (null != dest) {
dest.close();
}
}
}