/**
* 将多个excel文件压缩成一个zip文件
* @param excelPath:存放excel文件的路径或目录
* @param zipPath:存放压缩后的zip文件的路径或目录
* @param isDeleteExcel:压缩后是否删除原excel文件,为true时删除,否则不删除
*/
public void doZipExcel(String excelPath,String zipPath,boolean isDeleteExcel){
if(excelPath==null || "".equals(excelPath.trim())){
throw new RuntimeException("excelPath为空!");
}
if(zipPath==null || "".equals(zipPath.trim())){
throw new RuntimeException("zipPath为空!");
}
//存放所有excel文件的绝对路径
Map excelFileNamePath=new HashMap();
File excelFile=new File(excelPath);
//若excelPath为目录,则获得该目录下直接的所有excel文件
if(excelFile.isDirectory()){
File[] files= excelFile.listFiles();
for(int i=0;i<files.length;i++){
if(files[i].isFile()){
String fileName=files[i].getName();
String threadId=String.valueOf(Thread.currentThread().getId()).toLowerCase();
if(fileName.toLowerCase().indexOf(threadId)!=-1 && fileName.toLowerCase().endsWith(".xls")){
String excelFilePath=files[i].getAbsolutePath();
String excelName=files[i].getName();
excelFileNamePath.put(excelName, excelFilePath);
}
}
}
}else{
String excelFilePath=excelFile.getAbsolutePath();
String excelName=excelFile.getName();
excelFileNamePath.put(excelName, excelFilePath);
}
File zipFile=new File(zipPath);
//若zipPath为路径,则在该路径下生成默认的文件名称
if(zipFile.isDirectory()){
if(zipPath.endsWith(File.separator)){
zipPath=zipPath+Thread.currentThread().getId()+"_excels.zip";
}else{
zipPath=zipPath+File.separator+Thread.currentThread().getId()+"_excels.zip";
}
}
//将所有的excel文件都压缩成一个zip文件
try {
FileOutputStream fos=new FileOutputStream(zipPath);
ZipOutputStream zipos=new ZipOutputStream(new BufferedOutputStream(fos));
Iterator excelFileNamePathItor=excelFileNamePath.entrySet().iterator();
while(excelFileNamePathItor.hasNext()){
Map.Entry excelFileNamePathEntry=(Map.Entry)excelFileNamePathItor.next();
String excelFileName=(String)excelFileNamePathEntry.getKey();
String excelFilePath=(String)excelFileNamePathEntry.getValue();
zipos.putNextEntry(new ZipEntry(excelFileName));
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(excelFilePath));
int c;
while((c=bis.read())!=-1){
zipos.write(c);
zipos.flush();
}
bis.close();
}
zipos.close();
} catch (Exception e) {
throw new RuntimeException("excel文件压缩出错!");
}
//压缩后删除原excel文件
if(isDeleteExcel){
Iterator excelFileNamePathItor=excelFileNamePath.entrySet().iterator();
while(excelFileNamePathItor.hasNext()){
Map.Entry excelFileNamePathEntry=(Map.Entry)excelFileNamePathItor.next();
String excelFilePath=(String)excelFileNamePathEntry.getValue();
File excelF=new File(excelFilePath);
if(excelF.exists()){
excelF.delete();
}
}
}
}
/**
*
* java解压缩zip文件
*
*/
public class ZipUtil {
public static void main(String[] args) {
zip("D://ExcelExport", "D://Excels.zip");
unZip ( "D://Excels.zip", "G://ExcelExport" );
}
/**
* 功能:把 sourceDir 目录下的所有文件进行 zip 格式的压缩,保存为指定 zip 文件
* @param sourceDir:待压缩的源文件的目录
* @param zipFile:压缩文件的存放目录
*/
public static void zip(String sourceDir, String zipFile) {
OutputStream os;
try {
os = new FileOutputStream(zipFile);
BufferedOutputStream bos = new BufferedOutputStream(os);
ZipOutputStream zos = new ZipOutputStream(bos);
File file = new File(sourceDir);
String basePath = null ;
if (file.isDirectory()) {
basePath = file.getPath();
} else {
basePath = file.getParent();
}
zipFile (file, basePath, zos);
zos.closeEntry();
zos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 压缩文件
* @param source:待压缩的源文件的File对象
* @param basePath:待压缩的源文件的基目录
* @param zos:ZipOutputStream对象
* @throws IOException
*/
private static void zipFile(File source, String basePath,ZipOutputStream zos) {
File[] files = new File[0];
if (source.isDirectory()) {
files = source.listFiles();
} else {
files = new File[1];
files[0] = source;
}
String pathName;
byte [] buf = new byte [1024];
int length = 0;
try {
for (File file : files) {
if (file.isDirectory()) {
pathName = file.getPath().substring(basePath.length() + 1)+ "/" ;
zos.putNextEntry( new ZipEntry(pathName));
zipFile (file, basePath, zos);
} else {
pathName = file.getPath().substring(basePath.length() + 1);
InputStream is = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
zos.putNextEntry( new ZipEntry(pathName));
while ((length = bis.read(buf)) > 0) {
zos.write(buf, 0, length);
zos.flush();
}
is.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 解压 zip 文件
* @param zipfile:待解压的zip文件
* @param destDir:解压后文件的存放目录
* @throws IOException
*/
public static void unZip(String zipfile, String destDir) {
destDir = destDir.endsWith( "//" ) ? destDir : destDir + "//" ;
byte b[] = new byte [1024];
int length;
ZipFile zipFile;
try {
zipFile = new ZipFile( new File(zipfile));
Enumeration enumeration = zipFile.entries();
ZipEntry zipEntry = null ;
while (enumeration.hasMoreElements()) {
zipEntry = (ZipEntry) enumeration.nextElement();
System.out.println(zipEntry.getName());
File loadFile = new File(destDir + zipEntry.getName());
if (zipEntry.isDirectory()) {
// 这段都可以不要,因为每次都貌似从最底层开始遍历的
loadFile.mkdirs();
} else {
if (!loadFile.getParentFile().exists())
loadFile.getParentFile().mkdirs();
OutputStream outputStream = new FileOutputStream(loadFile);
InputStream inputStream = zipFile.getInputStream(zipEntry);
while ((length = inputStream.read(b)) > 0)
outputStream.write(b, 0, length);
}
}
System. out .println( " 文件解压成功 " );
} catch (IOException e) {
e.printStackTrace();
}
}
}
* 将多个excel文件压缩成一个zip文件
* @param excelPath:存放excel文件的路径或目录
* @param zipPath:存放压缩后的zip文件的路径或目录
* @param isDeleteExcel:压缩后是否删除原excel文件,为true时删除,否则不删除
*/
public void doZipExcel(String excelPath,String zipPath,boolean isDeleteExcel){
if(excelPath==null || "".equals(excelPath.trim())){
throw new RuntimeException("excelPath为空!");
}
if(zipPath==null || "".equals(zipPath.trim())){
throw new RuntimeException("zipPath为空!");
}
//存放所有excel文件的绝对路径
Map excelFileNamePath=new HashMap();
File excelFile=new File(excelPath);
//若excelPath为目录,则获得该目录下直接的所有excel文件
if(excelFile.isDirectory()){
File[] files= excelFile.listFiles();
for(int i=0;i<files.length;i++){
if(files[i].isFile()){
String fileName=files[i].getName();
String threadId=String.valueOf(Thread.currentThread().getId()).toLowerCase();
if(fileName.toLowerCase().indexOf(threadId)!=-1 && fileName.toLowerCase().endsWith(".xls")){
String excelFilePath=files[i].getAbsolutePath();
String excelName=files[i].getName();
excelFileNamePath.put(excelName, excelFilePath);
}
}
}
}else{
String excelFilePath=excelFile.getAbsolutePath();
String excelName=excelFile.getName();
excelFileNamePath.put(excelName, excelFilePath);
}
File zipFile=new File(zipPath);
//若zipPath为路径,则在该路径下生成默认的文件名称
if(zipFile.isDirectory()){
if(zipPath.endsWith(File.separator)){
zipPath=zipPath+Thread.currentThread().getId()+"_excels.zip";
}else{
zipPath=zipPath+File.separator+Thread.currentThread().getId()+"_excels.zip";
}
}
//将所有的excel文件都压缩成一个zip文件
try {
FileOutputStream fos=new FileOutputStream(zipPath);
ZipOutputStream zipos=new ZipOutputStream(new BufferedOutputStream(fos));
Iterator excelFileNamePathItor=excelFileNamePath.entrySet().iterator();
while(excelFileNamePathItor.hasNext()){
Map.Entry excelFileNamePathEntry=(Map.Entry)excelFileNamePathItor.next();
String excelFileName=(String)excelFileNamePathEntry.getKey();
String excelFilePath=(String)excelFileNamePathEntry.getValue();
zipos.putNextEntry(new ZipEntry(excelFileName));
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(excelFilePath));
int c;
while((c=bis.read())!=-1){
zipos.write(c);
zipos.flush();
}
bis.close();
}
zipos.close();
} catch (Exception e) {
throw new RuntimeException("excel文件压缩出错!");
}
//压缩后删除原excel文件
if(isDeleteExcel){
Iterator excelFileNamePathItor=excelFileNamePath.entrySet().iterator();
while(excelFileNamePathItor.hasNext()){
Map.Entry excelFileNamePathEntry=(Map.Entry)excelFileNamePathItor.next();
String excelFilePath=(String)excelFileNamePathEntry.getValue();
File excelF=new File(excelFilePath);
if(excelF.exists()){
excelF.delete();
}
}
}
}
/**
*
* java解压缩zip文件
*
*/
public class ZipUtil {
public static void main(String[] args) {
zip("D://ExcelExport", "D://Excels.zip");
unZip ( "D://Excels.zip", "G://ExcelExport" );
}
/**
* 功能:把 sourceDir 目录下的所有文件进行 zip 格式的压缩,保存为指定 zip 文件
* @param sourceDir:待压缩的源文件的目录
* @param zipFile:压缩文件的存放目录
*/
public static void zip(String sourceDir, String zipFile) {
OutputStream os;
try {
os = new FileOutputStream(zipFile);
BufferedOutputStream bos = new BufferedOutputStream(os);
ZipOutputStream zos = new ZipOutputStream(bos);
File file = new File(sourceDir);
String basePath = null ;
if (file.isDirectory()) {
basePath = file.getPath();
} else {
basePath = file.getParent();
}
zipFile (file, basePath, zos);
zos.closeEntry();
zos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 压缩文件
* @param source:待压缩的源文件的File对象
* @param basePath:待压缩的源文件的基目录
* @param zos:ZipOutputStream对象
* @throws IOException
*/
private static void zipFile(File source, String basePath,ZipOutputStream zos) {
File[] files = new File[0];
if (source.isDirectory()) {
files = source.listFiles();
} else {
files = new File[1];
files[0] = source;
}
String pathName;
byte [] buf = new byte [1024];
int length = 0;
try {
for (File file : files) {
if (file.isDirectory()) {
pathName = file.getPath().substring(basePath.length() + 1)+ "/" ;
zos.putNextEntry( new ZipEntry(pathName));
zipFile (file, basePath, zos);
} else {
pathName = file.getPath().substring(basePath.length() + 1);
InputStream is = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(is);
zos.putNextEntry( new ZipEntry(pathName));
while ((length = bis.read(buf)) > 0) {
zos.write(buf, 0, length);
zos.flush();
}
is.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 解压 zip 文件
* @param zipfile:待解压的zip文件
* @param destDir:解压后文件的存放目录
* @throws IOException
*/
public static void unZip(String zipfile, String destDir) {
destDir = destDir.endsWith( "//" ) ? destDir : destDir + "//" ;
byte b[] = new byte [1024];
int length;
ZipFile zipFile;
try {
zipFile = new ZipFile( new File(zipfile));
Enumeration enumeration = zipFile.entries();
ZipEntry zipEntry = null ;
while (enumeration.hasMoreElements()) {
zipEntry = (ZipEntry) enumeration.nextElement();
System.out.println(zipEntry.getName());
File loadFile = new File(destDir + zipEntry.getName());
if (zipEntry.isDirectory()) {
// 这段都可以不要,因为每次都貌似从最底层开始遍历的
loadFile.mkdirs();
} else {
if (!loadFile.getParentFile().exists())
loadFile.getParentFile().mkdirs();
OutputStream outputStream = new FileOutputStream(loadFile);
InputStream inputStream = zipFile.getInputStream(zipEntry);
while ((length = inputStream.read(b)) > 0)
outputStream.write(b, 0, length);
}
}
System. out .println( " 文件解压成功 " );
} catch (IOException e) {
e.printStackTrace();
}
}
}