熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Java程序實現壓縮某目錄

2022-06-13   來源: Java核心技術 

  public class TestZip {

  public static String SERPEROT = /;

  public static int BUFFER = ;

  public static void main(String args[]){

  zip(e:/hello/ e:/hellozip);

  }

  public static void zip(String srcFile String descFile){

  ZipOutputStream zos = null;

  FileOutputStream fos = null;

  File file = null;

  try {

  fos = new FileOutputStream(descFile);

  zos = new ZipOutputStream(fos);

  file = new File(srcFile);

  String folder = srcFilesubstring(srcFilelastIndexOf(/) +srcFilelength());

  zip(zosfilefolder);

  } catch (FileNotFoundException e) {

  eprintStackTrace();

  }finally{

  try{

  if(zos != null){zosclose();}

  if(fos != null){fosclose();}

  }catch(Exception e){

  eprintStackTrace();

  }

  }

  }

  private static void zip(ZipOutputStream descFile File srcFileString srcfolder){

  FileInputStream fis = null;

  Systemoutprintln(srcFileisDirectory());

  try{

  if(srcFileisDirectory()){

  File[] files = srcFilelistFiles();

  descFileputNextEntry(new ZipEntry(srcfolder + /)); //是壓縮包裡面的路徑

  srcfolder = srcfolderlength() == ? : srcfolder + /;

  Systemoutprintln(srcfolder);

  for(int i= ; i<fileslength; i++){

  zip(descFilefiles[i]srcfolder + files[i]getName());

  }

  }else{

  descFileputNextEntry(new ZipEntry(srcfolder));

  fis = new FileInputStream(srcFile);

  byte[] bytes = new byte[];

  int n =;

  while((n = fisread(bytes)) != ){

  descFilewrite(bytesn);

  }

  }

  }catch(Exception e){

  eprintStackTrace();

  }finally{

  try{

  if(fis != null){fisclose();}

  }catch(Exception e){

  eprintStackTrace();

  }

  }

  }

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/25760.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.