您的位置:首页 > 编程语言 > Java开发

java 将文件打成zip压缩包

2017-05-24 14:05 435 查看
import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

public class ZipUtil {

    public static void zip(File inputFile, String zipFileName) {

        try {

            // 创建文件输出对象out,提示:注意中文支持

            FileOutputStream out = new FileOutputStream(new String(

                    zipFileName.getBytes("UTF-8")));

            // 將文件輸出ZIP输出流接起来

            ZipOutputStream zOut = new ZipOutputStream(out);

            zip(zOut, inputFile, "");

            zOut.close();

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

    public static void zip(ZipOutputStream zOut, File file, String base) {

        try {

            // 如果文件句柄是目录

            if (file.isDirectory()) {

                // 获取目录下的文件

                File[] listFiles = file.listFiles();

                // 建立ZIP条目

                zOut.putNextEntry(new ZipEntry(base + "/"));

                base = (base.length() == 0 ? "" : base + "/");

                // 遍历目录下文件

                for (int i = 0; i < listFiles.length; i++) {

                    // 递归进入本方法

                    zip(zOut, listFiles[i], base + listFiles[i].getName());

                }

            }

            // 如果文件句柄是文件

            else {

                if (base == "") {

                    base = file.getName();

                }

                // 填入文件句柄

                zOut.putNextEntry(new ZipEntry(base));

                // 开始压缩

                // 从文件入流读,写入ZIP 出流

                writeFile(zOut, file);

            }

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

    public static void writeFile(ZipOutputStream zOut, File file)

            throws IOException {

        FileInputStream in = new FileInputStream(file);

        int len;

        while ((len = in.read()) != -1)

            zOut.write(len);

        in.close();

    }

    public static void main(String[] args) {

         zip(new File("e:/test2"),

         "e:/ziptest.zip");

    }

    //删除路径下所有文件

    public static boolean delAllFile(String path) {

        boolean flag = false;

        File file = new File(path);

        if (!file.exists()) {

            return flag;

        }

        if (!file.isDirectory()) {

            return flag;

        }

        String[] tempList = file.list();

        File temp = null;

        for (int i = 0; i < tempList.length; i++) {

            if (path.endsWith(File.separator)) {

                temp = new File(path + tempList[i]);

            } else {

                temp = new File(path + File.separator + tempList[i]);

            }

            if (temp.isFile()) {

                temp.delete();

            }

            if (temp.isDirectory()) {

                delAllFile(path + "/" + tempList[i]);// 先删除文件夹里面的文件

                delFolder(path + "/" + tempList[i]);// 再删除空文件夹

                flag = true;

            }

        }

        return flag;

    }

    public static void delFolder(String folderPath) {

        try {

            delAllFile(folderPath); // 删除完里面所有内容

            String filePath = folderPath;

            filePath = filePath.toString();

            java.io.File myFilePath = new java.io.File(filePath);

            myFilePath.delete(); // 删除空文件夹

        } catch (Exception e) {

            e.printStackTrace();

        }

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: