用JAVA寫一個壓縮檔的例子

來源:互聯網
上載者:User

public class Gzip {
 private static Log LOG = LogFactory.getLog(Config.class);
 public final static SimpleDateFormat Date_FORMAT = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
   /**
     * @param path 要壓縮的路徑, 可以是目錄, 也可以是檔案.
     * @param basePath 如果path是目錄,它一般為new File(path), 作用是:使輸出的zip檔案以此目錄為根目錄, 如果為null它只壓縮檔, 不解壓目錄.
     * @param zo 壓縮輸出資料流
     * @param isRecursive 是否遞迴
     * @param isOutBlankDir 是否輸出空目錄, 要使輸出空目錄為true,同時baseFile不為null.
     * @throws IOException
     */
   public static void main(String[] args){
    IndexerToZip("D://makeindexs","D://makeindexs","makeindexs_bak");
   }
   public static void zip(String path, File basePath, ZipOutputStream zo, boolean isRecursive, boolean isOutBlankDir) throws IOException {
        File inFile = new File(path);
        File[] files = new File[0];
        if(inFile.isDirectory()) {    //是目錄
            files = inFile.listFiles(new KeywordFileFilter(".lock",false));
        } else if(inFile.isFile()) {    //是檔案
            files = new File[1];
            files[0] = inFile;
        }
        byte[] buf = new byte[1024];
        int len;
        //System.out.println("baseFile: "+baseFile.getPath());
        for(int i=0; i<files.length; i++) {
            String pathName = "";
            if(basePath != null) {
                if(basePath.isDirectory()) {
                    pathName = files[i].getPath().substring(basePath.getPath().length()+1);
                } else {//檔案
                    pathName = files[i].getPath().substring(basePath.getParent().length()+1);
                }
            } else {
                pathName = files[i].getName();
            }
            System.out.println(pathName);
            if(files[i].isDirectory()) {
                if(isOutBlankDir && basePath != null) {   
                    zo.putNextEntry(new ZipEntry(pathName+"/"));    //可以使空目錄也放進去
                }
                if(isRecursive) {    //遞迴
                    zip(files[i].getPath(), basePath, zo, isRecursive, isOutBlankDir);
                }
            } else {
                FileInputStream fin = new FileInputStream(files[i]);
                zo.putNextEntry(new ZipEntry(pathName));
                while((len=fin.read(buf))>0) {
                    zo.write(buf,0,len);
                }
                fin.close();
            }
        }
    }
  
   public static boolean IndexerToZip(String frompath,String tobasepath,String zipname)
   {
    if(!new File(frompath).isDirectory())return false;  
       OutputStream os;
  try {
   tobasepath = FilePath.fixPath(tobasepath);
    if(!new File(tobasepath).exists())
     new File(tobasepath).mkdirs();
      String SavePath = new File(tobasepath).getParent() +"/"+zipname+Date_FORMAT.format(Calendar.getInstance().getTime())+".zip";   
   if(!new File(SavePath).exists()) new File(SavePath).createNewFile();
   os = new FileOutputStream(SavePath);
         BufferedOutputStream bs = new BufferedOutputStream(os);
         ZipOutputStream zo = new ZipOutputStream(bs);
         zip(frompath, new File(frompath), zo, true, true);        
         zo.closeEntry();
         zo.close();
         return true;
  } catch (Exception e) {
   LOG.debug("IndexerToZip Err:"+e.getMessage());
   return false;
  }

   }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.