JAva antパッケージで書かれたzip圧縮ファイルのツールクラス

3552 ワード


/**
*  apache  ant 
*maven   
* 
org.apache.ant
ant
1.10.2

*
*
*/
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

import java.io.*;



public class FileUtil {
    /**
     *     
     * @param srcFileName:         
     * @param zipFileName:       
     */
    public static void compressFile(String srcFileName, String zipFileName) {
        File srcFile=new File(srcFileName);

        if(!srcFile.exists())
        {
            return;
        }
        File zipFile=new File(zipFileName);

        if(!zipFile.exists())
        {
            zipFile.getParentFile().mkdirs();//           ,     
        }
        ZipOutputStream zos= null;
        try {
            zos = new ZipOutputStream(new FileOutputStream(zipFile));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        if(srcFile.isFile())
        {
            //    
            compressFile(srcFile,zos,"");
        }else
        {
            //    
            String baseDir=srcFileName+File.separator;
            compressDirectory(srcFile,zos,baseDir);
        }
        try {
            zos.flush();
            zos.close();//zip    ,     ,               
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    /**
     *     
     * @param srcFile
     * @param zos
     * @param baseDir
     */
    private static void compressFile(File srcFile, ZipOutputStream zos,String baseDir) {
        //      
        if("".equals(baseDir))
        {
            try {
                zos.putNextEntry(new ZipEntry(srcFile.getName()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //         ,     ,            
        else
        {
            try {
                zos.putNextEntry(new ZipEntry(baseDir+srcFile.getName()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            //         ,    
            BufferedInputStream bufferedInputStream=new BufferedInputStream(new FileInputStream(srcFile));
            byte[] buffer=new byte[2048];
            int len;
            while((len=bufferedInputStream.read(buffer))!=-1)
            {
                //  zip       
                zos.write(buffer,0,len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *     
     * @param srcFile
     * @param zos
     * @param baseDir
     */
    private static void compressDirectory(File srcFile,ZipOutputStream zos,String baseDir)
    {
       File[] files=srcFile.listFiles();
       for(File file:files)
       {
           if(file.isFile())
           {
               /**
                *     
                */
               compressFile(file,zos,baseDir);
           }else
           {
               /**
                *     
                */
               compressDirectory(file,zos,baseDir+file.getName()+File.separator);
           }
       }
    }
    public static  void main(String[] args) {
  
  
  compressFile("D:"+File.separator+"test.txt","D:"+File.separator+"testFile.zip");
compressFile("D:"+File.separator+"android-21","D"+File.separator+"testDirectory.zip");

    }
}