java產生壓縮檔範例程式碼_java

來源:互聯網
上載者:User

代碼:

複製代碼 代碼如下:

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

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

/**
 * @project: Test
 * @author chenssy
 * @date 2013-7-28
 * @Description: 檔案壓縮公用程式類
 *                   將指定檔案/檔案夾壓縮成zip、rar壓縮檔
 */
public class CompressedFileUtil {
    /**
     * 預設建構函式
     */
    public CompressedFileUtil(){

    }

    /**
     * @desc 將源檔案/檔案夾產生指定格式的壓縮檔,格式zip
     * @param resourePath 源檔案/檔案夾
     * @param targetPath  目的壓縮檔儲存路徑
     * @return void
     * @throws Exception
     */
    public void compressedFile(String resourcesPath,String targetPath) throws Exception{
        File resourcesFile = new File(resourcesPath);     //源檔案
        File targetFile = new File(targetPath);           //目的
        //如果目的路徑不存在,則建立
        if(!targetFile.exists()){    
            targetFile.mkdirs(); 
        }

        String targetName = resourcesFile.getName()+".zip";   //目的壓縮檔名
        FileOutputStream outputStream = new FileOutputStream(targetPath+"\\"+targetName);
        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(outputStream));

        createCompressedFile(out, resourcesFile, "");

        out.close(); 
    }

    /**
     * @desc 產生壓縮檔。
     *                  如果是檔案夾,則使用遞迴,進行檔案遍曆、壓縮
     *       如果是檔案,直接壓縮
     * @param out  輸出資料流
     * @param file  目標檔案
     * @return void
     * @throws Exception
     */
    public void createCompressedFile(ZipOutputStream out,File file,String dir) throws Exception{
        //如果當前的是檔案夾,則進行進一步處理
        if(file.isDirectory()){
            //得到檔案清單資訊
            File[] files = file.listFiles();
            //將檔案夾添加到下一級打包目錄
            out.putNextEntry(new ZipEntry(dir+"/"));

            dir = dir.length() == 0 ? "" : dir +"/";

            //迴圈將檔案夾中的檔案打包
            for(int i = 0 ; i < files.length ; i++){
                createCompressedFile(out, files[i], dir + files[i].getName());         //遞迴處理
            }
        }
        else{   //當前的是檔案,打包處理
            //檔案輸入資料流
            FileInputStream fis = new FileInputStream(file);

            out.putNextEntry(new ZipEntry(dir));
            //進行寫操作
            int j =  0;
            byte[] buffer = new byte[1024];
            while((j = fis.read(buffer)) > 0){
                out.write(buffer,0,j);
            }
            //關閉輸入資料流
            fis.close();
        }
    }

    public static void main(String[] args){
        CompressedFileUtil compressedFileUtil = new CompressedFileUtil();

        try {
            compressedFileUtil.compressedFile("G:\\zip", "F:\\zip");
            System.out.println("壓縮檔已經產生...");
        } catch (Exception e) {
            System.out.println("壓縮檔產生失敗...");
            e.printStackTrace();
        }
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.