J2EE社區(qū)

          茍有恒,何必三更起五更眠;
          最無益,只怕一日曝十日寒.
          posts - 241, comments - 318, trackbacks - 0, articles - 16

           

          package com.wepull.demo;

           

          import java.io.*;

          import java.util.zip.*;

           

          /**

           * 對(duì)文件或者目錄操作的類

           * 
          @version 1.0

           * 
          @author leno

           
          */


          public class FileUtil {

           

             
          private static void copy(File source, File target) throws IOException {

                File tar 
          = new File(target, source.getName());

                
          if (source.isDirectory()) {

                  System.out.println(
          "開始創(chuàng)建目錄:" + tar.getPath());

                  tar.mkdir();

                  File[] fs 
          = source.listFiles();

                  
          for (int i = 0; i < fs.length; i++{

                     copy(fs[i], tar);

                  }


                }
           else {

                  System.out.println(
          "開始從" + source + "拷貝文件到" + tar.getPath());

                  InputStream is 
          = new FileInputStream(source);

                  OutputStream os 
          = new FileOutputStream(tar);

                  
          byte[] buf = new byte[1024];

                  
          int len = 0;

                  
          while ((len = is.read(buf)) != -1{

                     os.write(buf, 
          0, len);

                  }


                  is.close();

                  os.close();

                }


             }


           

             
          /**

              * 拷貝文件或者目錄到某個(gè)指定的路徑

              * 

              * 
          @param source

              *            源文件或者目錄

              * 
          @param target

              *            目標(biāo)路徑

              * 
          @throws IOException

              
          */


             
          public static void copy(String source, String target) {

                File sour 
          = new File(source);

                File tar 
          = new File(target);

                
          try {

                  copy(sour, tar);

                }
           catch (IOException e) {

                  
          // TODO Auto-generated catch block

                  e.printStackTrace();

                }


             }


           

             
          private static void delete(File file) {

                
          if (file.isDirectory()) {

                  File[] fs 
          = file.listFiles();

                  
          for (int i = 0; i < fs.length; i++{

                     delete(fs[i]);

                  }


                  file.delete();

                }
           else {

                  file.delete();

                }


             }


           

             
          /**

              * 刪除一個(gè)文件或者目錄

              * 

              * 
          @param file

              
          */


             
          public static void delete(String path) {

                File file 
          = new File(path);

                delete(file);

             }


           

             
          /**

              * 壓縮文件或者目錄到指定的路徑

              * 

              * 
          @param zipFileName

              *            目標(biāo)路徑

              * 
          @param inputPath

              *            被壓縮的文件或者目錄

              
          */


             
          public static void zip(String zipFileName, String inputPath) {

                File inputFile 
          = new File(inputPath);

                ZipOutputStream out;

                
          try {

                  out 
          = new ZipOutputStream(new FileOutputStream(zipFileName));

                  zip(out, inputFile, inputFile.getName());

                  System.out.println(
          "壓縮完成!");

                  out.close();

                }
           catch (Exception e) {

                  
          // TODO Auto-generated catch block

                  e.printStackTrace();

                }


             }


           

             
          private static void zip(ZipOutputStream out, File f, String base)

                  
          throws Exception {

                System.out.println(
          "正在壓縮:" + f.getName() + " ");

                
          if (f.isDirectory()) {

                  File[] fs 
          = f.listFiles();

                  base 
          += "/";

                  System.out.println(
          "新建目錄條目:" + f.getName());

                  out.putNextEntry(
          new ZipEntry(base)); // 生成相應(yīng)的目錄

                  
          for (int i = 0; i < fs.length; i++{

                     
          // 對(duì)本目錄下的所有文件對(duì)象遞歸調(diào)用本方法

                     zip(out, fs[i], base 
          + fs[i].getName());

                  }


                }
           else {

                  System.out.println(
          "新增文件條目:" + f.getName());

                  out.putNextEntry(
          new ZipEntry(base));

                  InputStream is 
          = new FileInputStream(f);

                  
          byte[] buf = new byte[1024];

                  
          int len = 0;

                  
          while ((len = is.read(buf)) != -1{

                     out.write(buf, 
          0, len);

                  }


                  is.close();

                }


             }


           

             
          /**

              * 解壓縮zip文件到指定的路徑

              * 

              * 
          @param zipfile

              *            zip格式壓縮文件

              * 
          @param desPath

              *            目標(biāo)路徑

              
          */


             
          public static void unzip(String zipFile, String desPath) {

                
          // 建立輸出流,用于將從壓縮文件中讀出的文件流寫入到磁盤

                OutputStream out 
          = null;

                
          // 建立輸入流,用于從壓縮文件中讀出文件

                ZipInputStream is;

                
          try {

                  is 
          = new ZipInputStream(new FileInputStream(zipFile));

                  ZipEntry entry 
          = null;

                  
          while ((entry = is.getNextEntry()) != null{

                     System.out.println(
          "正在解壓縮:" + entry.getName() + " ");

                     File f 
          = new File(desPath + "\\" + entry.getName());

                     
          if (entry.isDirectory()) {

                        System.out.println(
          "新建目錄:" + f.getName());

                        f.mkdir();

                     }
           else {

                        System.out.println(
          "新增文件:" + f.getName());

                        
          // 根據(jù)壓縮文件中讀出的文件名稱新建文件

                        out 
          = new FileOutputStream(f);

                        
          byte[] buf = new byte[1024];

                        
          int len = 0;

                        
          while ((len = is.read(buf)) != -1{

                           out.write(buf, 
          0, len);

                        }


                        out.close();

                     }


                  }


                  is.close();

                }
           catch (Exception e) {

                  
          // TODO Auto-generated catch block

                  e.printStackTrace();

                }


             }


           

             
          /**

              * 創(chuàng)建新文件

              * 

              * 
          @param path

              
          */


             
          public static void createFile(String path) {

                File file 
          = new File(path);

                
          try {

                  file.createNewFile();

                }
           catch (IOException e) {

                  
          // TODO Auto-generated catch block

                  e.printStackTrace();

                }


           

             }


           

             
          /**

              * 創(chuàng)建新目錄

              * 

              * 
          @param path

              
          */


             
          public static void createDir(String path) {

                File file 
          = new File(path);

                file.mkdirs();

             }


           

             
          /**

              * 剪切文件或者目錄到某個(gè)指定的路徑

              * 

              * 
          @param source

              *            源文件或者目錄

              * 
          @param target

              *            目標(biāo)路徑

              * 

              
          */


             
          public static void cutTo(String source, String target) {

                File sourFile 
          = new File(source);

                File tarFile 
          = new File(target);

                
          if (sourFile.isFile()) {

                  
          if (tarFile.isDirectory()) {

                     sourFile.renameTo(tarFile);

                  }


                }
           else {

                  copy(source, target);

                  delete(source);

                }


             }


           

             
          public static void main(String[] args) {

                
          // copy("E:\\w.txt", "E:\\a");

                
          // delete("E:\\a");

                
          // zip("E:\\a.zip", "E:\\b");

                
          // unzip("E:\\a.zip", "E:\\b");

                
          // createFile("E:\\a.txt");

                
          // createDir("E:\\bb");

                
          // cutTo("E:\\b", "D:\\");

             }


          }
          本文來自CSDN博客,轉(zhuǎn)載請(qǐng)標(biāo)明出處:http://blog.csdn.net/lenotang/archive/2008/07/23/2698562.aspx



          名稱: ?4C.ESL | .↗Evon
          口號(hào): 遇到新問題?先要尋找一個(gè)方案乄而不是創(chuàng)造一個(gè)方案こ
          mail: 聯(lián)系我


          主站蜘蛛池模板: 株洲市| 永嘉县| 清新县| 嵊州市| 八宿县| 曲靖市| 龙岩市| 璧山县| 铜川市| 沙田区| 奉化市| 荥经县| 疏勒县| 阜新市| 铁岭县| 山东省| 全椒县| 永吉县| 武安市| 阿鲁科尔沁旗| 泗水县| 盐源县| 托克逊县| 永州市| 沂南县| 哈尔滨市| 博湖县| 永德县| 靖宇县| 和政县| 崇义县| 桦甸市| 尼玛县| 怀来县| 会泽县| 会宁县| 漳平市| 惠安县| 商城县| 江永县| 微山县|