您可以在这里快速查找:


 
您的位置: 编程学习 > java教程 > 200602
文章分类

Java技术
2005: 03 04 05 06 07 08
09 10 11 12
2006: 01 02

Asp.net
2005: 07 08 09 10 11 12
2006: 01 02

VB编程
2006: 02

Asp编程
2005: 11 12
2006: 01 02

C++/VC
2005: 10 11 12
2006: 01 02

Delphi
2005: 12
2006: 01 02

其它

 本文章适合所有读者

用Java压缩文件或目录下的所有文件

tyrone1979
import java.io.File;
import org.apache.tools.zip.ZipOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
 * @author tyrone
 *
 */
public class DirectoryZip {
 /**
  *@param inputFileName, file or directory waiting for zipping ,outputFileName output file name
  *
  */
 public void zip(String inputFileName,String outputFileName) throws Exception {
  ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outputFileName));
  zip(out, new File(inputFileName), "");
  System.out.println("zip done");
  out.close();
 }
 private void zip(ZipOutputStream out, File f, String base) throws Exception {
  if (f.isDirectory()) {
   File[] fl = f.listFiles();
   if (System.getProperty("os.name").startsWith("Windows")){
    out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "\\"));
    base = base.length() == 0 ? "" : base + "\\";
   }
   else{
    out.putNextEntry(new org.apache.tools.zip.ZipEntry(base + "/"));
    base = base.length() == 0 ? "" : base + "/";
   }
   for (int i = 0; i < fl.length; i++) {
    zip(out, fl[i], base + fl[i].getName());
   }
  }
  else {
   out.putNextEntry(new org.apache.tools.zip.ZipEntry(base));
   FileInputStream in = new FileInputStream(f);
   int b;
   System.out.println(base);
   while ( (b = in.read()) != -1) {
    out.write(b);
   }
   in.close();
  }
 }
 public static void main(String[] args){
  DirectoryZip m_zip=new DirectoryZip();
  try{
   m_zip.zip(args[0],"release\\2005.zip");
  }catch(Exception ex){
   ex.printStackTrace();
  }
 }
}