package com.rquest.riskmaster.util;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.zip.Zip64Mode;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.poi.ss.usermodel.Workbook;
public class CompressZipFileUtil {
/* public static void main(String[] args) {
String destFile = "D:\\compresstest\\comm.zip";
File file1 = new File("D:\\svn地址.jpg");
File file2 = new File("D:\\北农商项目代码training.docx");
File[] files = new File[]{file1,file2};
compressFiles2Zip(files, destFile);
}*/
/**
* 将excel文件存到指定位置
*
* @param filePath
* @param wb
*/
public static boolean writeExcelToDisk(String filePath, Workbook wb) {
try {
File file = new File(filePath);
file.getParentFile().mkdirs();
FileOutputStream fout = new FileOutputStream(filePath);
wb.write(fout);
fout.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* @param directory
* @return
*/
public static boolean deleteFiles(String directory){
File dir = new File(directory);
try {
if (dir.exists()) {
File[] listFiles = dir.listFiles();
for (int i = 0; i < listFiles.length; i++) {
listFiles[i].delete();
}
}else{
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 删除指定后缀的文件
* @param directory
* @param suffix
* @return
*/
public static boolean deleteFilesBySuffix(String directory,String suffix){
File dir = new File(directory);
try {
if (dir.exists()) {
File[] listFiles = dir.listFiles();
for (int i = 0; i < listFiles.length; i++) {
if (listFiles[i].getName().endsWith(suffix)) {
listFiles[i].delete();
}
}
}else{
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* @param file
* @return
*/
public static boolean write(File file){
OutputStream out = null;
try {
out = new FileOutputStream(file);
BufferedWriter writer = null;
writer = new BufferedWriter(new OutputStreamWriter(out, "GB2312"), 1024);
writer.flush();
writer.close();
} catch (IOException e1) {
e1.printStackTrace();
return false;
}
return true;
}
/**
* 将文件打包成zip压缩包文件
*
* @param files
* @param targetFilePath
* @return
*/
public static boolean compressFiles2Zip(File[] files, String targetFilePath) {
InputStream inputStream = null;
ZipArchiveOutputStream zipArchiveOutputStream = null;
try {
File zipFile = new File(targetFilePath);
zipFile.getParentFile().mkdirs();
zipArchiveOutputStream = new ZipArchiveOutputStream(zipFile);
// Use Zip64 extensions for all entries where they are required
zipArchiveOutputStream.setUseZip64(Zip64Mode.AsNeeded);
for (File file : files) {
// 将每个文件用ZipArchiveEntry封装,使用ZipArchiveOutputStream写到压缩文件
ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(file, file.getName());
zipArchiveOutputStream.putArchiveEntry(zipArchiveEntry);
inputStream = new FileInputStream(file);
byte[] buffer = new byte[1024 * 5];
int len = -1;
while ((len = inputStream.read(buffer)) != -1) {
// 把缓冲区的字节写入到ZipArchiveEntry
zipArchiveOutputStream.write(buffer, 0, len);
}
}
zipArchiveOutputStream.closeArchiveEntry();
zipArchiveOutputStream.finish();
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
// 关闭输入流
if (null != inputStream) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 关闭输出流
if (null != zipArchiveOutputStream) {
try {
zipArchiveOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}
/**
* 将zip压缩包解压成文件到指定文件夹
*
* @param zipFilePath
* @param targetDirPath
* @return
*/
public static boolean decompressZip2Files(String zipFilePath, String targetDirPath) {
InputStream inputStream = null;
OutputStream outputStream = null;
// zip文件输入流
ZipArchiveInputStream zipArchiveInputStream = null;
ArchiveEntry archiveEntry = null;
try {
File zipFile = new File(zipFilePath);
inputStream = new FileInputStream(zipFile);
zipArchiveInputStream = new ZipArchiveInputStream(inputStream, "UTF-8");
while (null != (archiveEntry = zipArchiveInputStream.getNextEntry())) {
// 获取文件名
String archiveEntryFileName = archiveEntry.getName();
// 构造解压后文件的存放路径
String archiveEntryPath = targetDirPath + archiveEntryFileName;
// 把解压出来的文件写到指定路径
File entryFile = new File(archiveEntryPath);
if (!entryFile.exists()) {
entryFile.getParentFile().mkdirs();
}
byte[] buffer = new byte[1024 * 5];
outputStream = new FileOutputStream(entryFile);
int len = -1;
while ((len = zipArchiveInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
outputStream.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
if (null != outputStream) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != zipArchiveInputStream) {
try {
zipArchiveInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != inputStream) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}
}
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.zip.Zip64Mode;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
import org.apache.poi.ss.usermodel.Workbook;
public class CompressZipFileUtil {
/* public static void main(String[] args) {
String destFile = "D:\\compresstest\\comm.zip";
File file1 = new File("D:\\svn地址.jpg");
File file2 = new File("D:\\北农商项目代码training.docx");
File[] files = new File[]{file1,file2};
compressFiles2Zip(files, destFile);
}*/
/**
* 将excel文件存到指定位置
*
* @param filePath
* @param wb
*/
public static boolean writeExcelToDisk(String filePath, Workbook wb) {
try {
File file = new File(filePath);
file.getParentFile().mkdirs();
FileOutputStream fout = new FileOutputStream(filePath);
wb.write(fout);
fout.close();
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* @param directory
* @return
*/
public static boolean deleteFiles(String directory){
File dir = new File(directory);
try {
if (dir.exists()) {
File[] listFiles = dir.listFiles();
for (int i = 0; i < listFiles.length; i++) {
listFiles[i].delete();
}
}else{
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* 删除指定后缀的文件
* @param directory
* @param suffix
* @return
*/
public static boolean deleteFilesBySuffix(String directory,String suffix){
File dir = new File(directory);
try {
if (dir.exists()) {
File[] listFiles = dir.listFiles();
for (int i = 0; i < listFiles.length; i++) {
if (listFiles[i].getName().endsWith(suffix)) {
listFiles[i].delete();
}
}
}else{
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* @param file
* @return
*/
public static boolean write(File file){
OutputStream out = null;
try {
out = new FileOutputStream(file);
BufferedWriter writer = null;
writer = new BufferedWriter(new OutputStreamWriter(out, "GB2312"), 1024);
writer.flush();
writer.close();
} catch (IOException e1) {
e1.printStackTrace();
return false;
}
return true;
}
/**
* 将文件打包成zip压缩包文件
*
* @param files
* @param targetFilePath
* @return
*/
public static boolean compressFiles2Zip(File[] files, String targetFilePath) {
InputStream inputStream = null;
ZipArchiveOutputStream zipArchiveOutputStream = null;
try {
File zipFile = new File(targetFilePath);
zipFile.getParentFile().mkdirs();
zipArchiveOutputStream = new ZipArchiveOutputStream(zipFile);
// Use Zip64 extensions for all entries where they are required
zipArchiveOutputStream.setUseZip64(Zip64Mode.AsNeeded);
for (File file : files) {
// 将每个文件用ZipArchiveEntry封装,使用ZipArchiveOutputStream写到压缩文件
ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(file, file.getName());
zipArchiveOutputStream.putArchiveEntry(zipArchiveEntry);
inputStream = new FileInputStream(file);
byte[] buffer = new byte[1024 * 5];
int len = -1;
while ((len = inputStream.read(buffer)) != -1) {
// 把缓冲区的字节写入到ZipArchiveEntry
zipArchiveOutputStream.write(buffer, 0, len);
}
}
zipArchiveOutputStream.closeArchiveEntry();
zipArchiveOutputStream.finish();
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
// 关闭输入流
if (null != inputStream) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
// 关闭输出流
if (null != zipArchiveOutputStream) {
try {
zipArchiveOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}
/**
* 将zip压缩包解压成文件到指定文件夹
*
* @param zipFilePath
* @param targetDirPath
* @return
*/
public static boolean decompressZip2Files(String zipFilePath, String targetDirPath) {
InputStream inputStream = null;
OutputStream outputStream = null;
// zip文件输入流
ZipArchiveInputStream zipArchiveInputStream = null;
ArchiveEntry archiveEntry = null;
try {
File zipFile = new File(zipFilePath);
inputStream = new FileInputStream(zipFile);
zipArchiveInputStream = new ZipArchiveInputStream(inputStream, "UTF-8");
while (null != (archiveEntry = zipArchiveInputStream.getNextEntry())) {
// 获取文件名
String archiveEntryFileName = archiveEntry.getName();
// 构造解压后文件的存放路径
String archiveEntryPath = targetDirPath + archiveEntryFileName;
// 把解压出来的文件写到指定路径
File entryFile = new File(archiveEntryPath);
if (!entryFile.exists()) {
entryFile.getParentFile().mkdirs();
}
byte[] buffer = new byte[1024 * 5];
outputStream = new FileOutputStream(entryFile);
int len = -1;
while ((len = zipArchiveInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, len);
}
outputStream.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
if (null != outputStream) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != zipArchiveInputStream) {
try {
zipArchiveInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != inputStream) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return true;
}
}