java 压缩文件
来源:互联网 发布:唐安琪 知乎 编辑:程序博客网 时间:2024/05/16 06:11
public static final String FILETYPE = ".zip";// 压缩文件类型
public static String compressedFile(String resourcePath, String targetPath, String fileName) {
File resourcesFile = new File(resourcePath);// 源文件路径
File targetFile = new File(targetPath); // 目标文件路径
if (!targetFile.exists()) {// 如果目标路径不存在,则新建
targetFile.mkdirs();
}
// 目的文件压缩名
UUID uuid = UUID.randomUUID();
String targetName = uuid.toString() + FILETYPE;
FileOutputStream outStream = null;
ZipOutputStream out = null;
try {
outStream = new FileOutputStream(targetPath + File.separator + targetName);
out = new ZipOutputStream(new BufferedOutputStream(outStream));
createCompressedFile(out, resourcesFile, "");
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();// 如果流不关闭,文件打包有错
} catch (IOException e) {
e.printStackTrace();
}
}
}
return targetName;
}
/**
* 生成压缩文件。 如果是文件夹,则使用递归,进行文件遍历、压缩 如果是文件,直接压缩
*
* @param out
* @param file
* @param dir
* @throws IOException
*/
public static void createCompressedFile(ZipOutputStream out, File file, String dir) {
FileInputStream fis = null;
try {
// 如果当前的是文件夹,则进行进一步处理
if (file.isDirectory()) {
// 得到文件列表信息
File[] files = file.listFiles();
// 将文件夹添加到下一级打包目录
//out.putNextEntry(new ZipEntry(dir + File.separator));
dir = dir.length() == 0 ? "" : dir + File.separator;
// 循环将文件夹中的文件打包
for (int i = 0; i < files.length; i++) {
createCompressedFile(out, files[i], dir + files[i].getName());// 递归处理
}
// 当前的是文件,打包处理
} else {
fis = new FileInputStream(file);
out.putNextEntry(new ZipEntry(dir));
int len = 0;
byte[] buffer = new byte[1024];
// 进行写操作
while ((len = fis.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static String compressedFile(String resourcePath, String targetPath, String fileName) {
File resourcesFile = new File(resourcePath);// 源文件路径
File targetFile = new File(targetPath); // 目标文件路径
if (!targetFile.exists()) {// 如果目标路径不存在,则新建
targetFile.mkdirs();
}
// 目的文件压缩名
UUID uuid = UUID.randomUUID();
String targetName = uuid.toString() + FILETYPE;
FileOutputStream outStream = null;
ZipOutputStream out = null;
try {
outStream = new FileOutputStream(targetPath + File.separator + targetName);
out = new ZipOutputStream(new BufferedOutputStream(outStream));
createCompressedFile(out, resourcesFile, "");
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();// 如果流不关闭,文件打包有错
} catch (IOException e) {
e.printStackTrace();
}
}
}
return targetName;
}
/**
* 生成压缩文件。 如果是文件夹,则使用递归,进行文件遍历、压缩 如果是文件,直接压缩
*
* @param out
* @param file
* @param dir
* @throws IOException
*/
public static void createCompressedFile(ZipOutputStream out, File file, String dir) {
FileInputStream fis = null;
try {
// 如果当前的是文件夹,则进行进一步处理
if (file.isDirectory()) {
// 得到文件列表信息
File[] files = file.listFiles();
// 将文件夹添加到下一级打包目录
//out.putNextEntry(new ZipEntry(dir + File.separator));
dir = dir.length() == 0 ? "" : dir + File.separator;
// 循环将文件夹中的文件打包
for (int i = 0; i < files.length; i++) {
createCompressedFile(out, files[i], dir + files[i].getName());// 递归处理
}
// 当前的是文件,打包处理
} else {
fis = new FileInputStream(file);
out.putNextEntry(new ZipEntry(dir));
int len = 0;
byte[] buffer = new byte[1024];
// 进行写操作
while ((len = fis.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
0 0
- Java压缩文件和解压缩文件
- java 压缩文件
- java压缩文件
- java压缩文件
- java 压缩文件
- java 压缩文件
- JAVA压缩文件
- java压缩文件
- JAVA 压缩文件
- java压缩文件
- java 压缩文件
- java压缩文件
- java 压缩文件
- java压缩文件
- Java压缩文件
- java压缩文件
- Java 压缩文件
- java压缩文件
- hdu 5082 Love(字符串处理)
- 移动端实现图片压缩上传
- java学习路线
- sass基础
- Day one
- java 压缩文件
- springmvc-hibernate配置
- 从汉诺塔到递归的探索和疑问
- (翻译)开始iOS 7中自动布局教程(二)
- PHP介绍
- ComboBox,ListBox,CheckedListBox控件
- 有序链表的合并操作
- Ubuntu 16.04下安装Tensorflow(GPU)
- C语言中的进制转换 itoa & sprintf