zip压缩多个路径的文件或目录到指定zip

package com.github.elizabetht.controller;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class Zip
{
    private static void zipDirectory(ZipOutputStream zos, String dirName,
                                     String basePath) throws Exception
    {
        File dir = new File(dirName);
        if (dir.exists())
        {
            File files[] = dir.listFiles();

            if (files.length > 0)
            {
                for (File file : files)
                {

                    if (file.isDirectory())
                    {
                        zipDirectory(zos, file.getPath(), basePath
                                + file.getName().substring(
                                file.getName().lastIndexOf(
                                        File.separator) + 1)
                                + File.separator);
                    }
                    else
                        zipFile(zos, file.getPath(), basePath);
                }
            }
            else
            {
                ZipEntry ze = new ZipEntry(basePath);
                zos.putNextEntry(ze);
            }
        }
    }

    private static void  zipFile(ZipOutputStream zos, String filename,
                                 String basePath) throws Exception
    {
        File file = new File(filename);

        if (file.exists())
        {

            FileInputStream fis = new FileInputStream(filename);
            ZipEntry ze = new ZipEntry(basePath + file.getName());

            zos.putNextEntry(ze);
            byte[] buffer = new byte[8192];
            int count = 0;
            while ((count = fis.read(buffer)) > 0)
            {
                zos.write(buffer, 0, count);
            }
            fis.close();
        }
    }

    public static void compress(String zipFilename, String... paths)
            throws Exception
    {
        compress(new FileOutputStream(zipFilename), paths);

    }

    public static void  compress(OutputStream os, String... paths)
            throws Exception
    {
        ZipOutputStream zos = new ZipOutputStream(os);

        for (String path : paths)
        {
            if(path.equals("")) continue;
            java.io.File file = new java.io.File(path);
            if (file.exists())
            {

                if (file.isDirectory())
                {
                    zipDirectory(zos, file.getPath(), file.getName() + File.separator);
                }
                else
                {
                    zipFile(zos, file.getPath(), "");
                }
            }
        }
        zos.close();
    }


    public static void main(String[] args) throws Exception{
        
	compress("E:/输出.zip","E:/草稿箱","E:/垃圾箱");
}}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值