展开全部
ZipEntry 类是java.util.zip包下的一个类,
ZipEntry 类用于表示 ZIP 文件条32313133353236313431303231363533e58685e5aeb931333337396230目。
利用这个类压缩和解压zip文件
具体压缩的例子如下: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;
/**
* 压缩程序
* @author young
*
*/
public class SingleFileZip {
public static void main(String[] args) {
File file = new File("e:/test.txt");
FileInputStream fis = null;
ZipOutputStream zos = null;
try {
fis = new FileInputStream(file);
zos = new ZipOutputStream(new FileOutputStream("e:/my.zip"));
// 创建压缩文件中的条目
ZipEntry entry = new ZipEntry(file.getName());
// 将创建好的条目加入到压缩文件中
zos.putNextEntry(entry);
// 写入当前条目所对应的具体内容
byte[] buff = new byte[1024];
int len = 0;
while ((len = fis.read(buff)) != -1) {
zos.write(buff, 0, len);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
fis.close();
zos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
解压例子如下: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.ZipFile;
import java.util.zip.ZipInputStream;
/**
* 解压程序
* @author young
*
*/
public class SingleFileUnZip {
public static void main(String[] args) {
FileOutputStream fos = null;
ZipInputStream zis = null;
InputStream is = null;
try {
ZipFile zf = new ZipFile("e:/my.zip");
zis = new ZipInputStream(new FileInputStream("e:/my.zip"));
fos = new FileOutputStream("e:/unzip.txt");
// 从压缩文件中获取一个条目
ZipEntry entry = zis.getNextEntry();
// 获得该条目对象的数据流
is = zf.getInputStream(entry);
byte[] buff = new byte[1024];
int len = 0;
while ((len = is.read(buff)) != -1) {
fos.write(buff, 0, len);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
is.close();
zis.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}