国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java生成壓縮文件示例代碼

2019-11-26 15:53:47
字體:
供稿:網(wǎng)友

代碼:

復(fù)制代碼 代碼如下:

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 {
    /**
     * 默認(rèn)構(gòu)造函數(shù)
     */
    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+"http://"+targetName);
        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(outputStream));

        createCompressedFile(out, resourcesFile, "");

        out.close(); 
    }

    /**
     * @desc 生成壓縮文件。
     *                  如果是文件夾,則使用遞歸,進(jìn)行文件遍歷、壓縮
     *       如果是文件,直接壓縮
     * @param out  輸出流
     * @param file  目標(biāo)文件
     * @return void
     * @throws Exception
     */
    public void createCompressedFile(ZipOutputStream out,File file,String dir) throws Exception{
        //如果當(dāng)前的是文件夾,則進(jìn)行進(jìn)一步處理
        if(file.isDirectory()){
            //得到文件列表信息
            File[] files = file.listFiles();
            //將文件夾添加到下一級(jí)打包目錄
            out.putNextEntry(new ZipEntry(dir+"/"));

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

            //循環(huán)將文件夾中的文件打包
            for(int i = 0 ; i < files.length ; i++){
                createCompressedFile(out, files[i], dir + files[i].getName());         //遞歸處理
            }
        }
        else{   //當(dāng)前的是文件,打包處理
            //文件輸入流
            FileInputStream fis = new FileInputStream(file);

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

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

        try {
            compressedFileUtil.compressedFile("G://zip", "F://zip");
            System.out.println("壓縮文件已經(jīng)生成...");
        } catch (Exception e) {
            System.out.println("壓縮文件生成失敗...");
            e.printStackTrace();
        }
    }
}

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 石家庄市| 通道| 二连浩特市| 宁陕县| 石柱| 咸丰县| 资中县| 临猗县| 海安县| 松江区| 扎鲁特旗| 洞头县| 平乡县| 景东| 库尔勒市| 榆树市| 垣曲县| 澎湖县| 合阳县| 黎平县| 光山县| 湖北省| 芜湖市| 民丰县| 民乐县| 拉萨市| 洛扎县| 茂名市| 如皋市| 西宁市| 长沙县| 武城县| 建湖县| 博兴县| 高密市| 延津县| 茌平县| 南召县| 大埔区| 海淀区| 英山县|