最近在学习java的io知识,写了一个简单地压缩和解压缩的程序,只能实现对文字型(像txt,world)文件的压缩,无法对图片等压缩,在这个过程中遇到的最大困难是空目录的处理,下面看代码
实现压缩文件
package io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
public class ZipOutputStreamDemo {
private File file;
private File zipFile;
private InputStream input;
private ZipOutputStream zipOut;
//构造函数
public ZipOutputStreamDemo(File file) throws FileNotFoundException {
this.file = file;
this.zipFile = new File (file.getAbsolutePath()+".zip");
InputStream input = null;
this.zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
}
//实现对文件(夹)的压缩
public void compressFile (File directory) throws IOException {
if (directory != null) {
if (directory.isDirectory()) {
File [] f = directory.listFiles();
if (f!=null) {
//目录里面文件数为0,即空目录,此时在压缩包中相对路径下创建一个空目录
if (f.length==0) {
this.zipOut.putNextEntry(new ZipEntry (this.getPath(directory)+File.separator));
}
//递归调用compressFile函数,找出所有的文件
else {
for (int i=0;i<f.length;i++) {
compressFile (f[i]);
}
}
}
}
//对找出的文件进行压缩处理
else {
System.out.println(directory);
this.input = new FileInputStream (directory);
this.zipOut.putNextEntry(new ZipEntry (this.getPath(directory)));
int temp = 0;
while ((temp=input.read())!=-1) {
this.zipOut.write(temp);
}
this.input.close();
}
}
}
//获得该文件在压缩包中的相对路径
public String getPath (File f) {
String str1 = this.file.getAbsolutePath();
int n1= str1.length();
String str2 = f.getAbsolutePath();
int n2= str2.length();
String str3 = this.file.getName();
int n3= str3.length();
String str = str2.substring(n1-n3, n2);
return str;
}
}
实现文件的解压缩
package io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
public class ZipInputStreamDemo {
private File file;
private File outFile;
private ZipInputStream zipInput;
private ZipFile zipFile;
private ZipEntry entry;
private InputStream input;
private OutputStream out;
//构造函数
public ZipInputStreamDemo(File file) throws ZipException, IOException {
this.file = file;
this.outFile = null;
this.zipFile = new ZipFile (file);
this.zipInput = new ZipInputStream(new FileInputStream(this.file));
this.entry = null;
this.input=null;
this.out=null;
}
//对压缩包中的文件进行解压缩
public void decompress () throws IOException {
//用getNextEntry获得压缩包中的下一个文件条目并解压缩,直到没有文件了结束循环
while ((this.entry=this.zipInput.getNextEntry())!=null) {
System.out.println("解压缩"+this.entry.getName()+"文件");
this.outFile = new File ("e:"+File.separator+this.entry.getName());
//如果父路径的文件夹不存在,就创建该文件夹
if (!this.outFile.getParentFile().exists()) {
this.outFile.getParentFile().mkdirs();
}
if (!outFile.exists()) {
//当该文件条目为一个空目录时,创建该空目录
if (this.entry.getName().substring(this.entry.getName().length()-1,this.entry.getName().length()).equals(File.separator)) {
outFile.mkdirs();
}
//在解压的路径下创建该文件条目对应的文件
else {
this.outFile.createNewFile();
}
}
if (!outFile.isDirectory()) {
this.input = this.zipFile.getInputStream(entry);
this.out=new FileOutputStream (this.outFile);
int temp = 0;
while ((temp=input.read())!=-1) {
out.write(temp);
}
this.input.close();
this.out.close();
}
}
}
}
代码亲测在eclipse下运行有效