压缩文件(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");
}
}
- 压缩文件(zip)--适用于目录下既有目录又有文件的情况
- java 将文件/目录下 zip压缩文件解压并删除压缩包
- java zip压缩文件和目录
- java压缩文件目录 为 zip
- 用Java压缩文件或目录下的所有文件
- 用Java压缩文件或目录下的所有文件
- 用Java压缩文件或目录下的所有文件
- 用Java压缩文件或目录下的所有文件
- 有没有方法在不root手机的情况下读取Data目录下的文件
- 用Java实现zip压缩文件和目录程序代码(二)
- linux 解压目录下所有的zip文件 find 、xargs
- 把一个目录下的几个目录及文件压缩到一个zip文件中
- linux下压缩文件或者目录
- zip文件目录解压
- 解压.zip文件到指定的目录
- c#创建多级目录的ZIP文件
- 快速获取zip的文件目录
- JAVA学习笔记_File类获取、复制当前目录下又有文件
- spring整合mybatis(入门级简单教程4)--扫描mapper类
- 让架构接地气,不再云里雾里
- spring整合mybatis(入门级简单教程5)--使用mybatis的API
- 任意2n个整数,从其中选出n个整数,使得选出的n个整数和同剩下的n个整数之和的差最小
- stripes framework(框架)入门教程(附案例分析)
- 压缩文件(zip)--适用于目录下既有目录又有文件的情况
- YII的自定义路由规则类的使用(初级)
- 揭秘12306技术改造(三):传统框架云化迁移到内存数据平台
- libevent编程之bufferevent使用
- Linux(C/C++)下的文件操作open、fopen与freopen http://blog.csdn.net/a656343072/article/details/40539889
- 哈夫曼树--链式结构(建立huffman树、编码、解码)
- 哈夫曼树--顺序结构(建立、编码、解码)
- 无向图--邻接矩阵、连接矩阵、深度遍历、广度遍历、生成树
- 拓扑排序