JAVA工具类-文件的压缩解压(递归子文件夹压缩)

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

public class ZipUtil {

   /* private static final String zipPath = "G:\\7777.zip";
    private static final String unzipPath = "C:\\temp\\Lemur\\";
    private static final String srcFiles = "G:\\Test";

    public static void main(String[] args){
        zipFile();
    }*/

    /**
     * @Description //TODO 压缩
     * @Param [zipPath(压缩到的地址), srcFiles(需要压缩的文件夹)]
     * @return void
     */
    public static void zipFile(String zipPath,String srcFiles){
        File file = new File(zipPath);
        if(file.exists())
            file.delete();
        zipFileWithTier(srcFiles, zipPath);
    }

    /**
     * @Description //TODO 解压
     * @Param [zipPath, unzipPath]
     * @return void
     */
    public void upZipFile(String zipPath,String unzipPath){
        try {
            unzipFilesWithTier(readFileByte(zipPath), unzipPath + File.separator);
        } catch (IOException e) {

            e.printStackTrace();
        }
    }

    /*
     * 压缩包含子文件夹的指定文件并将其存储到zipfile中
     * @param srcFiles: 文件将被压缩
     * @param zipPath: 存储zipfile的位置。
     */
    public static void zipFileWithTier(String srcFiles, String zipPath) {
        try {

            FileOutputStream zipFile = new FileOutputStream(zipPath);
            BufferedOutputStream buffer = new BufferedOutputStream(zipFile);
            ZipOutputStream out = new ZipOutputStream(buffer);
            zipFiles(srcFiles, out, "");
            out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /*
     * 递归指定文件还包含可能不包含任何文件的文件夹。
     * @param filePath: compress file
     * @param ZipOutputStream: zipfile的输出流.
     * @param prefix: 前缀指示建立层关系的文件的父文件夹名称.
     */
    public static void zipFiles(String filePath, ZipOutputStream out, String prefix)
            throws IOException {
        File file = new File(filePath);
        if (file.isDirectory()) {
            if (file.listFiles().length == 0) {
                ZipEntry zipEntry = new ZipEntry(prefix + file.getName() + "//");
                out.putNextEntry(zipEntry);
                out.closeEntry();
            } else {
                prefix += file.getName() + File.separator;
                for (File f : file.listFiles())
                    zipFiles(f.getAbsolutePath(), out, prefix);
            }
        } else {
            FileInputStream in = new FileInputStream(file);
            ZipEntry zipEntry = new ZipEntry(prefix + file.getName());
            out.putNextEntry(zipEntry);
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            out.closeEntry();
            in.close();
        }

    }

    /*
     * 解压缩文件还包含listFiles长度为0的文件夹。
     * @param bytes: 按字节数组显示的zipfile的内容.     *
     * @param prefix: 前缀是存储路径的根
     * @IOExcetion: the ioexception during unzipFiles.
     */
    public static void unzipFilesWithTier(byte[] bytes, String prefix) throws IOException {

        InputStream bais = new ByteArrayInputStream(bytes);
        ZipInputStream zin = new ZipInputStream(bais);
        ZipEntry ze;
        while ((ze = zin.getNextEntry()) != null) {
            if (ze.isDirectory()) {
                File file = new File(prefix + ze.getName());
                if (!file.exists())
                    file.mkdirs();
                continue;
            }
            File file = new File(prefix + ze.getName());
            if (!file.getParentFile().exists())
                file.getParentFile().mkdirs();
            ByteArrayOutputStream toScan = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];
            int len;
            while ((len = zin.read(buf)) > 0) {
                toScan.write(buf, 0, len);
            }
            byte[] fileOut = toScan.toByteArray();
            toScan.close();
            writeByteFile(fileOut, new File(prefix + ze.getName()));
        }
        zin.close();
        bais.close();
    }

    public static byte[] readFileByte(String filename) throws IOException {

        if (filename == null || filename.equals("")) {
            throw new NullPointerException("文件不存在!");
        }
        File file = new File(filename);
        long len = file.length();
        byte[] bytes = new byte[(int) len];

        BufferedInputStream bufferedInputStream = new BufferedInputStream(
                new FileInputStream(file));
        int r = bufferedInputStream.read(bytes);
        if (r != len)
            throw new IOException("Read file failure!");
        bufferedInputStream.close();

        return bytes;

    }

    public static String writeByteFile(byte[] bytes, File file) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(file);
            fos.write(bytes);
        } catch (FileNotFoundException e) {
            e.printStackTrace();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return "success";
    }



}

你可能感兴趣的:(基础,java,工具类)