压缩文件(zip)--适用于目录下既有目录又有文件的情况

来源:互联网 发布:智能健康数据分析图 编辑:程序博客网 时间:2024/05/18 17:55

package test;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import cn.edu.cqupt.beans.OutApply;
import cn.edu.cqupt.dao.OutApplyDAO;
import cn.edu.cqupt.service.query_business.ApplyFormOperation;
import cn.edu.cqupt.service.transact_business.ApplyHandleService;
import cn.edu.cqupt.util.MyDateFormat;

public class FinleyTest {

/**注:这个和下面的fileToZip基本相同,只是这里不会压缩到新的文件夹下面。这样比下面的方法好
* 将存放在sourceFilePath目录下的源文件,打包成fileName名称的ZIP文件,并存放到zipFilePath。
*
* @param sourceFilePath
* 待压缩的文件路径
* @param zipFilePath
* 压缩后存放路径
* @param fileName
* 压缩后文件的名称
* @return flag
* @author LiangYH
*/

public boolean fileToZip(String sourceFilePath, String zipFilePath,
String fileName) {
boolean flag = false;
File sourceFile = new File(sourceFilePath);

FileInputStream fis = null;
FileOutputStream fos = null;
ZipOutputStream zos = null;

if (sourceFile.exists() == false) {
System.out.println(">>>>>> 待压缩的文件目录:" + sourceFilePath
+ " 不存在. <<<<<<");
} else {
try {
File temp = new File(zipFilePath);
if (!temp.exists() && !temp.isDirectory()) {
temp.mkdir();
}

File zipFile = new File(zipFilePath + "/" + fileName);

// File[] sourceFiles = sourceFile.listFiles();

List<File> fileList = loadFileName(sourceFile);

if (fileList.size() == 0) {
System.out.println(">>>>>> 待压缩的文件目录:" + sourceFilePath
+ " 里面不存在文件,无需压缩. <<<<<<");
} else {
fos = new FileOutputStream(zipFile);
zos = new ZipOutputStream(fos);
for (int i = 0; i < fileList.size(); i++) {
File file = fileList.get(i);

String tempFileName = getEntryName(sourceFilePath, file);
int index = -1;
if((index = tempFileName.indexOf("/")) == -1){
index = tempFileName.indexOf("\\");
}
tempFileName = tempFileName.substring(index+1);
// 创建ZIP实体,并添加进压缩包
ZipEntry zipEntry = new ZipEntry(tempFileName);
zos.putNextEntry(zipEntry);
// 读取待压缩的文件并写进压缩包里
fis = new FileInputStream(file);

int read = 0;
byte[] b = new byte[1024];
while ((read = fis.read(b)) != -1) {
zos.write(b, 0, read);
}
}
flag = true;
}

} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}finally{
try {
if (zos != null)
zos.close();
if (fos != null)
fos.close();
if (fis != null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

return flag;
}

/**
* 和前面的方法基本相同,只是这里压缩到新的文件夹下面
* @param sourceFilePath
* @param zipFilePath
* @param fileName
* @return
*/
public boolean fileToZip2(String sourceFilePath, String zipFilePath,
String fileName) {
boolean flag = false;
File sourceFile = new File(sourceFilePath);

FileInputStream fis = null;
FileOutputStream fos = null;
ZipOutputStream zos = null;

if (sourceFile.exists() == false) {
System.out.println("待压缩的文件目录:"+sourceFilePath+" 不存在.");
} else {
try {
File temp = new File(zipFilePath);
if (!temp.exists() && !temp.isDirectory()) {
temp.mkdir();
}

File zipFile = new File(zipFilePath + "/" + fileName);

List<File> fileList = loadFileName(sourceFile);

if (fileList.size() == 0) {
System.out.println("待压缩的文件目录"+sourceFilePath+"里面不存在文件");
} else {
fos = new FileOutputStream(zipFile);
zos = new ZipOutputStream(fos);
for (int i = 0; i < fileList.size(); i++) {
File file = fileList.get(i);

String entryName = getEntryName(sourceFilePath, file);
// 创建ZIP实体,并添加进压缩包
ZipEntry zipEntry = new ZipEntry(entryName);

zos.putNextEntry(zipEntry);
// 读取待压缩的文件并写进压缩包里
fis = new FileInputStream(file);

int read = 0;
byte[] b = new byte[1024];
while ((read = fis.read(b)) != -1) {
zos.write(b, 0, read);
}
}
flag = true;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new RuntimeException(e);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e);
}finally{
try {
if (zos != null)
zos.close();
if (fos != null)
fos.close();
if (fis != null)
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

return flag;
}

//获取目录下面的所有文件
private List<File> loadFileName(File file) {
List<File> fileNameList = new ArrayList<File>();
if (file.isFile()) {
fileNameList.add(file);
}
if (file.isDirectory()) {
for (File f : file.listFiles()) {
//递归
fileNameList.addAll(loadFileName(f));
}
}
return fileNameList;
}

//得到将要压缩在压缩包内的路径
private static String getEntryName(String base,File file) {
File baseFile = new File(base);

String filename = file.getPath();

//这种情况:E:/A
//A的parentFile是E:/
if(baseFile.getParentFile().getParentFile()==null){
return filename.substring(baseFile.getParent().length());
}
return filename.substring(baseFile.getParent().length()+1);
}



public static void main(String args[]) throws IOException {
FinleyTest t = new FinleyTest();
t.fileToZip2("E:/A", "E:/", "Cd.zip");
}
}

0 0