Java SE: How to compress File(.zip)

1. Main Function

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collection;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.apache.log4j.Logger;

public class FileCompressUtil
{
    private static final Logger logger = Logger
            .getLogger(FileCompressUtil.class);

    private static final int BUFFER_SIZE = 2048;

    private FileCompressUtil()
    {
    }

    public static void compressFile(final Collection<String> fileNameList,
            final String zippedFileNameWithPath) throws IOException
    {
        logger.debug(String.format(
                "Start compressFile with fileList: [%s], target file: [%s]",
                fileNameList, zippedFileNameWithPath));

        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(
                zippedFileNameWithPath));

        byte[] buffer = new byte[BUFFER_SIZE];
        for (String filePath : fileNameList)
        {
            ZipEntry e = new ZipEntry(filePath);
            zos.putNextEntry(e);
            logger.debug(String.format("Start compressFile with file: [%s]",
                    filePath));
            FileInputStream in = new FileInputStream(filePath);
            int len;
            while ((len = in.read(buffer)) > 0)
            {
                zos.write(buffer, 0, len);
            }
            in.close();
            zos.closeEntry();
            logger.debug(String.format("Finished compressFile with file: [%s]",
                    filePath));
        }

        zos.close();
        logger.debug(String.format(
                "Finished compressFile with fileList: [%s], target file: [%s]",
                fileNameList, zippedFileNameWithPath));
    }

    public static void compressAndDeleteOrigFile(final Collection<String> fileNameList,
            final String zippedFileNameWithPath) throws IOException
    {
        compressFile(fileNameList, zippedFileNameWithPath);
        for (String filePath : fileNameList)
        {
            File file = new File(filePath);
            file.delete();
        }
    }
}

    Test case:

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class FileCompressUtilTest
{
    private Collection<String> fileNameList;
    private String zipFileName;

    @Before
    public void setUp()
    {
        fileNameList = new ArrayList<String>();
        String filePath1 = "src/test/resources/aaa.xlsx";
        String filePath2 = "src/test/resources/bbb.xlsx";
        String filePath3 = "src/test/resources/ccc.xlsx";
        fileNameList.add(filePath1);
        fileNameList.add(filePath2);
        fileNameList.add(filePath3);

        zipFileName = "src/test/resources/test_compress.zip";
    }

    @Test
    public void testCompressFile() throws IOException
    {
        FileCompressUtil.compressFile(fileNameList, zipFileName);
    }

    @After
    public void tearDown()
    {
        new File(zipFileName).delete();
    }
}

 

你可能感兴趣的:(Compress ZIP)