Java单文件下支持多目录压缩类
来源:互联网 发布:美国网络实名制吗 编辑:程序博客网 时间:2024/05/18 03:07
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* 多层级目录压缩至ZIP文件中
* @since
* @author Gloomy.H
*/
public class FileIntoZipUtil {
//输出文件绝对路径tmp.zip
private final static File outFile = new File("C:/tmp/tmp.zip");
private static File inFile = null;
/**
* 初始化,构建相应目录和文件
*/
public static void init(){
if(!outFile.exists()){
try {
if(!outFile.getParentFile().exists()){
outFile.getParentFile().mkdirs();
}
outFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
return;
}
}
}
public static void createZip() {
init();
//输入文件
inFile= new File("H:/test");
ZipOutputStream zipOut = null;
try {
zipOut = new ZipOutputStream(new FileOutputStream(outFile));
//递归压缩文件
convertZip(inFile, zipOut, null);
zipOut.close();
} catch (FileNotFoundException e) {
System.out.println("Not find the File");
} catch (IOException e) {
System.out.println("There is a Exception in IO");
}
}
public static void convertZip(File file,
ZipOutputStream zipOut,
String path) {
FileInputStream fin = null;
if (file.isDirectory()) {
File[] fileList = file.listFiles();
for (File f : fileList) {
if (null != path) {
convertZip(f, zipOut, path + "/" + file.getName());
} else {
convertZip(f, zipOut, file.getName());
}
}
} else {
try {
zipOut.putNextEntry(new ZipEntry(path + "/" + file.getName()));
fin = new FileInputStream(file);
int n = -1;
while ((n = fin.read()) != -1) {
zipOut.write(n);
}
zipOut.closeEntry();
fin.close();
} catch (IOException e) {
System.out.println("There is a Exception in IO");
e.printStackTrace();
return;
}
}
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/**
* 多层级目录压缩至ZIP文件中
* @since
* @author Gloomy.H
*/
public class FileIntoZipUtil {
//输出文件绝对路径tmp.zip
private final static File outFile = new File("C:/tmp/tmp.zip");
private static File inFile = null;
/**
* 初始化,构建相应目录和文件
*/
public static void init(){
if(!outFile.exists()){
try {
if(!outFile.getParentFile().exists()){
outFile.getParentFile().mkdirs();
}
outFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
return;
}
}
}
public static void createZip() {
init();
//输入文件
inFile= new File("H:/test");
ZipOutputStream zipOut = null;
try {
zipOut = new ZipOutputStream(new FileOutputStream(outFile));
//递归压缩文件
convertZip(inFile, zipOut, null);
zipOut.close();
} catch (FileNotFoundException e) {
System.out.println("Not find the File");
} catch (IOException e) {
System.out.println("There is a Exception in IO");
}
}
public static void convertZip(File file,
ZipOutputStream zipOut,
String path) {
FileInputStream fin = null;
if (file.isDirectory()) {
File[] fileList = file.listFiles();
for (File f : fileList) {
if (null != path) {
convertZip(f, zipOut, path + "/" + file.getName());
} else {
convertZip(f, zipOut, file.getName());
}
}
} else {
try {
zipOut.putNextEntry(new ZipEntry(path + "/" + file.getName()));
fin = new FileInputStream(file);
int n = -1;
while ((n = fin.read()) != -1) {
zipOut.write(n);
}
zipOut.closeEntry();
fin.close();
} catch (IOException e) {
System.out.println("There is a Exception in IO");
e.printStackTrace();
return;
}
}
}
}
0 0
- Java单文件下支持多目录压缩类
- java zip单文件或多文件 压缩和解压缩
- 单目录下多文件 makefile编写
- java实现文件目录压缩
- Java 压缩,解压zip文件(支持中文)
- 取目录下所有文件支持linux
- Java递归查找指定目录下的特定格式的文件 并压缩到指定目录下
- Java压缩(文件或目录)
- java 处理zip压缩/解压 文件/目录
- java 将文件/目录下 zip压缩文件解压并删除压缩包
- LINUX下目录遍历搜索文件程序,支持嵌套目录
- LINUX下目录遍历搜索文件程序,支持嵌套目录
- 读取目录下的文件将文件压缩
- PHP文件上传类(支持单文件上传,也支持多文件上传)
- PHP文件上传类(支持单文件上传,也支持多文件上传)
- PHP文件上传类(支持单文件上传,也支持多文件上传)
- java 把指定文件夹下的所有文件目录和文件都压缩到指定文件夹下
- Java 把文件/目录压缩成zip文件
- Linux 运维分析中的常用命令一点通
- [FUNC]粘贴时附加网址
- 服务(Service)——线程、AsyncTask
- linux下安装mysql-5.7.16(tar.gz)
- 我的日记
- Java单文件下支持多目录压缩类
- Linux中的ssh命令
- jieba安装
- 最大子段和问题
- Java 后台性能优化简要
- 内核中的时间
- Oracle 常用性能监控SQL语句
- mochiweb源码阅读3-消息交互
- 收集+设计模式/六大设计原则/类的特性