package day20;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
import org.junit.Test;
* zip压缩与解压缩文件
public class ZipDemo {
* 压缩
@Test
public void test() throws Exception {
* 压缩流
FileOutputStream fos = new FileOutputStream("E://TestCase//day20//demo.zip");
ZipOutputStream zout = new ZipOutputStream(fos);
* 压缩图片
* 文件输入流
FileInputStream fis = new FileInputStream("E://TestCase//day20//demo.jpg");
* 压缩条目
ZipEntry entry = new ZipEntry("picture.jpg");
* 放入下一个条目
zout.putNextEntry(entry);
byte[] buffer = new byte[1024];
int len = -1;
while((len = fis.read(buffer)) != -1) {
zout.write(buffer, 0, len);
}
* 关闭条目
zout.closeEntry();
fis.close();
* 压缩txt
fis = new FileInputStream("E://TestCase//day20//fiction.txt");
ZipEntry entry2 = new ZipEntry("fiction.txt");
zout.putNextEntry(entry2);
while((len = fis.read(buffer)) != -1) {
zout.write(buffer, 0, len);
}
zout.closeEntry();
zout.close();
fos.close();
fis.close();
System.out.println("end");
}
* 解压文件
@Test
public void test5() throws Exception {
FileInputStream fis = new FileInputStream("E://TestCase//day20//demo.zip");
ZipInputStream zins = new ZipInputStream(fis);
ZipEntry entry = null;
while((entry = zins.getNextEntry()) != null) {
FileOutputStream fos = new FileOutputStream("E://TestCase//day20//undo//" + entry.getName());
byte[] buffer = new byte[1024];
int len = -1;
while((len = zins.read(buffer)) != -1) {
fos.write(buffer, 0, len);
}
fos.close();
zins.closeEntry();
}
zins.close();
fis.close();
}
}
网友评论