- 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.ZipInputStream;
- import java.util.zip.ZipOutputStream;
- /**
- * 打包zip
- * 打zip包和解zip包的简单程序
- */
- public class ZipTest {
- public static void main(String[] args) throws Exception{
- zip();
- unzip();
- }
- //生成zip文件
- public static void zip() throws IOException{
- ZipOutputStream zip =
- new ZipOutputStream(new FileOutputStream("d:/abc.zip"));
- File f = new File("d:/aa/a.txt");
- ZipEntry ze = new ZipEntry(f.getName());
- ze.setTime(f.lastModified());
- zip.putNextEntry(ze);
- InputStream in = new FileInputStream(f);
- byte[] b = new byte[1024];
- while(in.read(b)!=-1){
- zip.write(b);
- }
- zip.flush();
- zip.close();
- }
- //解开zip文件
- public static void unzip() throws IOException{
- ZipInputStream zip =
- new ZipInputStream(new FileInputStream("d:/abc.zip"));
- ZipEntry ze = zip.getNextEntry();
- OutputStream out = new FileOutputStream("d:/"+ze.getName());
- byte[] b = new byte[1024];
- while(zip.read(b)!=-1){
- out.write(b);
- }
- out.close();
- zip.close();
- }
- }
复制代码 很简单的小练习
|