java打包下载(新增递归压缩,指定路径压缩)

java打包下载,下面的工具类新增了递归压缩,指定路径压缩的方法



import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;


/**
 * 
 * 压缩文件工具类
 * 
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class ZipUtils
{
    
    /**
     * 
     * 压缩文件夹或单个文件
     * 
     * @param srcFile 目录或者单个文件url
     * @param zipFile 压缩后的ZIP文件url
     * @throws Exception
     * @see [类、类#方法、类#成员]
     */
    public static void doCompress(String srcFile, String zipFile)
        throws Exception
    {
        doCompress(new File(srcFile), new File(zipFile));
    }
    
    /**
     * 
     * 压缩文件夹或单个文件
     * 
     * @param srcFile 目录或者单个文件
     * @param destFile 压缩后的ZIP文件
     * @throws Exception
     * @see [类、类#方法、类#成员]
     */
    public static void doCompress(File srcFile, File destFile)
        throws Exception
    {
        ZipOutputStream out = new ZipOutputStream(new FileOutputStream(destFile));
        if (srcFile.isDirectory())
        {
            File[] files = srcFile.listFiles();
            for (File file : files)
            {
                doCompress(file, out);
            }
        }
        else
        {
            doCompress(srcFile, out);
        }
    }
    
    /**
     * 
     * 压缩单个文件
     * 
     * @param pathname 文件完整url
     * @param out ZipOutputStream
     * @throws IOException
     * @see [类、类#方法、类#成员]
     */
    public static void doCompress(String pathname, ZipOutputStream out)
        throws IOException
    {
        doCompress(new File(pathname), out);
    }
    
    /**
     * 
     * 压缩单个文件
     * 
     * @param file File实体类
     * @param out ZipOutputStream
     * @throws IOException
     * @see [类、类#方法、类#成员]
     */
    public static void doCompress(File file, ZipOutputStream out)
        throws IOException
    {
        if (file.exists())
        {
            byte[] buffer = new byte[1024];
            FileInputStream fis = new FileInputStream(file);
            out.putNextEntry(new ZipEntry(file.getName()));
            int len = 0;
            // 读取文件的内容,打包到zip文件
            while ((len = fis.read(buffer)) > 0)
            {
                out.write(buffer, 0, len);
            }
            out.flush();
            out.closeEntry();
            fis.close();
        }
    }
    
    /**
     * 递归压缩文件
     * 
     * @param source 源路径,可以是文件,也可以目录
     * @param destinct 目标路径,压缩文件名
     * @throws IOException
     */
    @SuppressWarnings("rawtypes")
    public static void compressRecursion(String source, String destinct)
        throws IOException
    {
        List fileList = loadFilename(new File(source));
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File(destinct)));
        
        byte[] buffere = new byte[8192];
        int length;
        BufferedInputStream bis;
        
        for (int i = 0; i < fileList.size(); i++)
        {
            File file = (File)fileList.get(i);
            zos.putNextEntry(new ZipEntry(getEntryName(source, file)));
            bis = new BufferedInputStream(new FileInputStream(file));
            
            while (true)
            {
                length = bis.read(buffere);
                if (length == -1)
                    break;
                zos.write(buffere, 0, length);
            }
            bis.close();
            zos.closeEntry();
        }
        zos.close();
    }
    
    /**
     * 递归压缩文件
     * 
     * @param source 要压缩的文件夹或文件的列表
     * @param destinct 目标路径,压缩文件名
     * @throws IOException
     */
    @SuppressWarnings("rawtypes")
    public static void compressRecursion(List<String> primaryPath, String destinct)
        throws IOException
    {
        
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File(destinct)));
        for (String path : primaryPath)
        {
            List fileList = loadFilename(new File(path));
            
            byte[] buffere = new byte[8192];
            int length;
            BufferedInputStream bis;
            
            for (int i = 0; i < fileList.size(); i++)
            {
                File file = (File)fileList.get(i);
                zos.putNextEntry(new ZipEntry(getEntryName(path, file)));
                bis = new BufferedInputStream(new FileInputStream(file));
                
                while (true)
                {
                    length = bis.read(buffere);
                    if (length == -1)
                        break;
                    zos.write(buffere, 0, length);
                }
                bis.close();
                zos.closeEntry();
            }
        }
        zos.close();
        
    }
    
    /**
     * 递归压缩文件
     * 
     * @param source 要压缩的文件夹或文件的列表
     * @param destinct 目标路径,压缩文件名
     * @throws IOException
     */
    @SuppressWarnings("rawtypes")
    public static void compressRecursion(List<String> primaryPath, ZipOutputStream zos)
        throws IOException
    {
        
        for (String path : primaryPath)
        {
            List fileList = loadFilename(new File(path));
            
            byte[] buffere = new byte[8192];
            int length;
            BufferedInputStream bis;
            
            for (int i = 0; i < fileList.size(); i++)
            {
                File file = (File)fileList.get(i);
                zos.putNextEntry(new ZipEntry(getEntryName(path, file)));
                bis = new BufferedInputStream(new FileInputStream(file));
                
                while (true)
                {
                    length = bis.read(buffere);
                    if (length == -1)
                        break;
                    zos.write(buffere, 0, length);
                }
                bis.close();
                zos.closeEntry();
            }
        }
        
        zos.close();
        
    }
    
    /**
     * 
     * 递归压缩文件并重命名
     * @param zipFiles
     *          文件路径新文件名封装实体
     * @param zos
     * @throws IOException
     * @see [类、类#方法、类#成员]
     */
    @SuppressWarnings("rawtypes")
    public static void compressRecursionRename(List<ZipFile> zipFiles, ZipOutputStream zos)
        throws IOException
    {
        int j=0;
        for (ZipFile zipFile : zipFiles)
        {
            j++;
            List fileList = loadFilename(new File(zipFile.getPhysicalFilePath()));
            
            byte[] buffere = new byte[8192];
            int length;
            BufferedInputStream bis;
            
            for (int i = 0; i < fileList.size(); i++)
            {
                File file = (File)fileList.get(i);
                zos.putNextEntry(new ZipEntry((j)+"_"+zipFile.getNodeTypefileName()));
                bis = new BufferedInputStream(new FileInputStream(file));
                
                while (true)
                {
                    length = bis.read(buffere);
                    if (length == -1)
                        break;
                    zos.write(buffere, 0, length);
                }
                
                bis.close();
                zos.closeEntry();
            }
        }
        
        zos.close();
    }
    
    /**
     * 递归获得该文件下所有文件名(不包括目录名)
     * 
     * @param file
     * @return
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public static List loadFilename(File file)
    {
        List filenameList = new ArrayList();
        if (file.isFile())
        {
            filenameList.add(file);
        }
        if (file.isDirectory())
        {
            for (File f : file.listFiles())
            {
                filenameList.addAll(loadFilename(f));
            }
        }
        return filenameList;
    }
    
    /**
     * 获得zip entry 字符串
     * 
     * @param base
     * @param file
     * @return
     */
    public static String getEntryName(String base, File file)
    {
        File baseFile = new File(base);
        String filename = file.getPath();
        // int index=filename.lastIndexOf(baseFile.getName());
        if (baseFile.getParentFile().getParentFile() == null)
            return filename.substring(baseFile.getParent().length());
        return filename.substring(baseFile.getParent().length() + 1);
    }
    
    /**
     * 
     * 压缩指定路径的文件
     * 
     * @param primaryPath 指定的所有文件路径
     * @param zipFilePath 要解压到的文件路径以及全文件名
     * @throws FileNotFoundException,Exception
     * @see [类、类#方法、类#成员]
     */
    public static void compressUseAppointPath(List<String> primaryPath, String zipFilePath)
        throws FileNotFoundException, Exception
    {
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFilePath));
        byte[] buffere = new byte[8192];
        int length;
        BufferedInputStream bis;
        
        for (String path : primaryPath)
        {
            File file = new File(path);
            zos.putNextEntry(new ZipEntry(file.getName()));
            bis = new BufferedInputStream(new FileInputStream(file));
            
            while (true)
            {
                length = bis.read(buffere);
                if (length == -1)
                    break;
                zos.write(buffere, 0, length);
            }
            bis.close();
            zos.closeEntry();
        }
        zos.close();
    }
    
    /**
     * 
     * 压缩指定文件夹的所有文件——不带文件夹
     * 
     * @param primaryPath
     * @param zipFilePath
     * @throws FileNotFoundException,Exception
     * @see [类、类#方法、类#成员]
     */
    public static void compressUseAppointDirPath(List<String> primaryPath, String zipFilePath)
        throws FileNotFoundException, Exception
    {
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFilePath));
        byte[] buffere = new byte[8192];
        int length;
        BufferedInputStream bis;
        
        for (String path : primaryPath)
        {
            File file = new File(path);
            if (file.isFile())
            {
                zos.putNextEntry(new ZipEntry(file.getName()));
                bis = new BufferedInputStream(new FileInputStream(file));
                
                while (true)
                {
                    length = bis.read(buffere);
                    if (length == -1)
                        break;
                    zos.write(buffere, 0, length);
                }
                bis.close();
                zos.closeEntry();
            }
            if (file.isDirectory())
            {
                for (File f : file.listFiles())
                {
                    zos.putNextEntry(new ZipEntry(f.getName()));
                    bis = new BufferedInputStream(new FileInputStream(f));
                    
                    while (true)
                    {
                        length = bis.read(buffere);
                        if (length == -1)
                            break;
                        zos.write(buffere, 0, length);
                    }
                    bis.close();
                    zos.closeEntry();
                }
            }
            
        }
        zos.close();
    }
    
    public static void main(String[] args)
        throws FileNotFoundException, Exception
    {
        
        List<String> primaryPath = new ArrayList<String>();
        primaryPath.add("D:/hsfile/contractfile/PJ1503902085962/2a9a091485214259bdc3e184811de72e");
        primaryPath.add("D:\\test1.csv");
        primaryPath.add("D:\\aaa.xls");
        primaryPath.add("D:\\temp2.pdf");
        compressUseAppointDirPath(primaryPath, "D:\\mytest.zip");
    }
    
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值