java压缩

     /* @description:压缩文件操作
     * @param filePath  要压缩的文件路径
     * @param descDir   压缩文件保存的路径 d:\aaa.zip
     */
    public static void zipFiles(String filePath, String descDir)
    {
        ZipOutputStream zos = null;
        System.out.println("******************ZipOutputStream********************");
        try
        {
            // 创建一个Zip输出流
            zos = new ZipOutputStream(new FileOutputStream(descDir));
            // 启动压缩
            startZip(zos, "", filePath);

            System.out.println("******************压缩完毕********************");
        }
        catch (IOException e)
        {
            // 压缩失败,则删除创建的文件
            File zipFile = new File(descDir);

            if (zipFile.exists())
            {
                zipFile.delete();
            }
            System.out.println("******************压缩失败********************");
            
            e.printStackTrace();
        }
        finally
        {
            try
            {
                if (zos != null)
                {
                    zos.close();
                }
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }

    /*
     * 
     * @description:对目录中所有文件递归遍历进行压缩
     * @param zos ZIP压缩输出流
     * @param oppositePath  在zip文件中的相对路径
     * @param directory   要压缩的文件的路径
     * @throws IOException
     */
    private static void startZip(ZipOutputStream zos, String oppositePath,
            String directory) throws IOException
    {
        File file = new File(directory);

        if (file.isDirectory())
        {
            // 如果是压缩目录
            File[] files = file.listFiles();

            for (int i = 0; i < files.length; i++)
            {
                File aFile = files[i];

                if (aFile.isDirectory())
                {
                    // 如果是目录,修改相对地址
                    String newOppositePath = oppositePath + aFile.getName() + "/";
                    // 压缩目录,这是关键,创建一个目录的条目时,需要在目录名后面加多一个"/"
                    ZipEntry entry = new ZipEntry(oppositePath + aFile.getName() + "/");

                    zos.putNextEntry(entry);

                    zos.closeEntry();
                    // 进行递归调用
                    startZip(zos, newOppositePath, aFile.getPath());
                }
                else
                {
                    // 如果不是目录,则进行压缩
                    zipFile(zos, oppositePath, aFile);
                }
            }
        }
        else
        {
            // 如果是压缩文件,直接调用压缩方法进行压缩
            zipFile(zos, oppositePath, file);
        }
    }

    /**
     * 
     * @description:压缩单个文件到目录中
     * @param zos  zip输出流
     * @param oppositePath 在zip文件中的相对路径
     * @param file 要压缩的的文件
     */
    private static void zipFile(ZipOutputStream zos, String oppositePath,File file)
    {
        // 创建一个Zip条目,每个Zip条目都是必须相对于根路径
        InputStream is = null;

        try
        {
            ZipEntry entry = new ZipEntry(oppositePath + file.getName());
            // 将条目保存到Zip压缩文件当中
            zos.putNextEntry(entry);
            // 从文件输入流当中读取数据,并将数据写到输出流当中.
            is = new FileInputStream(file);

            int length = 0;

            int bufferSize = 1024;

            byte[] buffer = new byte[bufferSize];

            while ((length = is.read(buffer, 0, bufferSize)) >= 0)
            {
                zos.write(buffer, 0, length);
            }
            zos.closeEntry();
        }
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
        finally
        {
            try
            {
                if (is != null)
                {
                    is.close();
                }
            }
            catch (IOException ex)
            {
                ex.printStackTrace();
            }
        }
    }
原文地址:https://www.cnblogs.com/ike_li/p/4961836.html