文件处理(压缩与解压)
来源:互联网 发布:starry nigh观星软件 编辑:程序博客网 时间:2024/06/02 05:15
ZipInputStream
ZipOutputStream(ZipEntry)
编写一个程序,在键盘上输入"Hello World", aaa.txt文件中,并将aaa.txt压缩到d:/aaa.zip文件中
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
System.out.println("输入的内容为: " +s);
try {
BufferedWriter bw = new BufferedWriter(new FileWriter("d:\\aaa.txt"));
bw.write(s);
bw.flush();
bw.close();
}、、、
压缩:
book.zip("D:\\MyZipTest.zip",new File("D:\\aaa.txt"));
//inputFile,要压缩的文件或者文件夹
//zipFileName,压缩后的zip文件名称
private void zip(String zipFileName,File inputFile) throws IOException,Exception{
System.out.println("正在压缩中、、、");
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFileName));
BufferedOutputStream bo = new BufferedOutputStream(out);
zip(out,inputFile,inputFile.getName(),bo);
bo.close();
out.close();//输出流关闭
System.out.println("压缩完成、、、");
}
//方法重载
private void zip(ZipOutputStream out ,File f ,String str,BufferedOutputStream bo) throws IOException,Exception{
if(f.isDirectory()){//测试此抽象路径名表示的文件是否是一个目录
File[] fl = f.listFiles();// 返回一个抽象路径名数组,这些路径名表示此抽象路径名表示的目录中的文件
if(fl.length == 0){
out.putNextEntry(new ZipEntry(str + "/"));//创建zip压缩进入点str
System.out.println(str + "/");
}
for(int i =0;i < fl.length;i++){
zip(out,fl[i],str + "/" + fl[i].getName(),bo);//递归遍历子文件夹
}
System.out.println("第 " + k + " 次递归");
k++;
}else {
out.putNextEntry(new ZipEntry(str));//创建zip压缩进入点str
System.out.println(str);
FileInputStream in = new FileInputStream(f);
BufferedInputStream bi = new BufferedInputStream(in);
int b;
while((b = bi.read()) != -1){
bo.write(b);//将字节流写入当前zip目录
}
bi.close();
in.close();//输出流关闭
}
解压:
ZipInputStream Zin = new ZipInputStream(new FileInputStream("D:\\MyZipTest.zip"));//输入源zip路径
BufferedInputStream Bin = new BufferedInputStream(Zin);
String Parent="D:\\bbb";//输出路径(文件夹目录)
File Fout=null;
ZipEntry entry;
try {
while((entry = Zin.getNextEntry())!=null && !entry.isDirectory()){
Fout=new File(Parent,entry.getName());
if(!Fout.exists()){
(new File(Fout.getParent())).mkdirs();
}
FileOutputStream out = new FileOutputStream(Fout);
BufferedOutputStream Bout = new BufferedOutputStream(out);
int b;
while((b = Bin.read())!=-1){
Bout.write(b);
}
Bout.close();
out.close();
System.out.println(Fout + "解压成功");
}
Bin.close();
Zin.close();
- 文件处理(压缩与解压)
- 文件压缩与解压
- 文件压缩与解压
- 文件压缩与解压
- 文件压缩与解压
- 文件压缩与解压
- 文件压缩与解压
- 文件压缩与解压
- Python 压缩与解压文件(二)
- java压缩与解压文件
- Linux文件压缩与解压
- Java文件解压与压缩
- Hadoop文件压缩与解压
- Linux文件压缩与解压
- C# 文件压缩与解压
- 文件的压缩与解压
- java文件压缩与解压
- linux文件压缩与解压
- openmp学习
- Pointers on C——8 Arrays.21
- 解决fullpage尾屏半屏问题
- java07_几张图了解count(*)与count(列名)的区别
- TLS/SSL 协议详解 (24) CFCA蛋疼的事
- 文件处理(压缩与解压)
- Pointers on C——8 Arrays.22
- K-means聚类算法详解
- java 解压tar.gz示例代码
- dex2oat
- Myeclipse的web项目导入eclipse中部署
- 动脉硬化检测分析部分指标说明
- Git更新远程仓库代码到本地 git fetch
- Java-DatabaseConnectionPool工具类