Greedy的工作室

posts - 23,comments - 7,trackbacks - 0

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class TestZip {
 static final int BUFFER = 40960;
 public static void main(String argv[]) {
 try {
  
 BufferedInputStream origin = null;
 FileOutputStream dest = new FileOutputStream("E:/test/myfiles.zip");//压缩后保存路径和文件名
 ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
 byte data[] = new byte[BUFFER];
 File f = new File("e:/test/a/");//压缩文件夹名
 File files[] = f.listFiles();
 for (int i = 0; i < files.length; i++) {
 FileInputStream fi = new FileInputStream(files[i]);
 origin = new BufferedInputStream(fi, BUFFER);
 ZipEntry entry = new ZipEntry(files[i].getName());
 out.putNextEntry(entry);
 int count;
 while ((count = origin.read(data, 0, BUFFER)) != -1) {
 out.write(data, 0, count);
 }
 origin.close();
 }
 out.close();
 } catch (Exception e) {
 e.printStackTrace();
 }
 }}

posted on 2008-11-27 21:34 greedy 阅读(158) 评论(0)  编辑  收藏 所属分类: Java技术

只有注册用户登录后才能发表评论。


网站导航: