J2EE社區

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

           

          package com.wepull.demo;

           

          import java.io.*;

          import java.util.zip.*;

           

          /**

           * 對文件或者目錄操作的類

           * 
          @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(
          "開始創建目錄:" + 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();

                }


             }


           

             
          /**

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

              * 

              * 
          @param source

              *            源文件或者目錄

              * 
          @param target

              *            目標路徑

              * 
          @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();

                }


             }


           

             
          /**

              * 刪除一個文件或者目錄

              * 

              * 
          @param file

              
          */


             
          public static void delete(String path) {

                File file 
          = new File(path);

                delete(file);

             }


           

             
          /**

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

              * 

              * 
          @param zipFileName

              *            目標路徑

              * 
          @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)); // 生成相應的目錄

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

                     
          // 對本目錄下的所有文件對象遞歸調用本方法

                     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

              *            目標路徑

              
          */


             
          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());

                        
          // 根據壓縮文件中讀出的文件名稱新建文件

                        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();

                }


             }


           

             
          /**

              * 創建新文件

              * 

              * 
          @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();

                }


           

             }


           

             
          /**

              * 創建新目錄

              * 

              * 
          @param path

              
          */


             
          public static void createDir(String path) {

                File file 
          = new File(path);

                file.mkdirs();

             }


           

             
          /**

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

              * 

              * 
          @param source

              *            源文件或者目錄

              * 
          @param target

              *            目標路徑

              * 

              
          */


             
          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博客,轉載請標明出處:http://blog.csdn.net/lenotang/archive/2008/07/23/2698562.aspx



          名稱: ?4C.ESL | .↗Evon
          口號: 遇到新問題?先要尋找一個方案乄而不是創造一個方案こ
          mail: 聯系我


          主站蜘蛛池模板: 历史| 吕梁市| 松溪县| 宣汉县| 隆德县| 同仁县| 宁津县| 资阳市| 乐山市| 卓资县| 东海县| 永顺县| 遂宁市| 乡城县| 横山县| 华阴市| 留坝县| 延安市| 苍梧县| 鄂州市| 信阳市| 榆树市| 沧源| 通城县| 阿荣旗| 桂平市| 巴楚县| 娄底市| 和田县| 达拉特旗| 巩留县| 兰溪市| 湖南省| 桑日县| 乌兰浩特市| 隆德县| 凤山市| 搜索| 淮北市| 平湖市| 隆回县|