• java生成zip包兼容Linux


    /*

    这个方法只用在windows中用服务器为Linux就不行

    */

    package common.util;

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

    public class ZipUtils {
    /** 压缩单个文件*/
    public static void zipFile(String filepath ,String zippath) {

    //String s = UUID.randomUUID().toString();为了怕后面删除这个临时文件是删除到同一个文件

    //这里filepath为要打包的文件夹的路径String filepath = ServletActionContext.getServletContext().getRealPath("upload/notice/"+s+"/");

    //zippath为生成的zip包的路径String zippath = ServletActionContext.getServletContext().getRealPath("upload/notice/"+s+"/week-job.zip");这里生成的就是个week-job.zip包

    //ServletActionContext.getServletContext().getRealPath();获得服务器地址
    InputStream input = null;
    ZipOutputStream zipOut = null;
    try {
    File file = new File(filepath);
    File zipFile = new File(zippath);
    input = new FileInputStream(file);
    zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
    zipOut.putNextEntry(new ZipEntry(file.getName()));
    int temp = 0;
    while((temp = input.read()) != -1){
    zipOut.write(temp);
    }
    System.out.println("zip "+filepath+" to "+zippath);
    } catch (Exception e) {
    e.printStackTrace();
    }finally{
    try {
    input.close();
    zipOut.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }

    /** 一次性压缩多个文件,文件存放至一个文件夹中*/
    public static void zipMultiFile(String filepath ,String zippath) {
    try {
    File file = new File(filepath);// 要被压缩的文件夹
    File zipFile = new File(zippath);
    InputStream input = null;
    ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
    if(file.isDirectory()){
    File[] files = file.listFiles();
    for(int i = 0; i < files.length; ++i){
    input = new FileInputStream(files[i]);
    zipOut.putNextEntry(new ZipEntry(file.getName() + File.separator + files[i].getName()));
    int temp = 0;
    while((temp = input.read()) != -1){
    zipOut.write(temp);
    }
    input.close();
    }
    }else{//否则,则调用压缩单个文件的方法
    zipFile(filepath, zippath);
    }
    zipOut.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    }

    ---------------------------------------------------------华丽的分割线----------------------------------------------------------

    /*

    兼容Linux的压缩方法

    */

    /*
    * Copyright 2017 BangChen Information Technology Ltd., Co.
    * Licensed under the Apache License 2.0.
    */
    package common.util;

    import java.io.File;
    import java.util.ArrayList;
    import java.util.Collections;
    import net.lingala.zip4j.core.ZipFile;
    import net.lingala.zip4j.exception.ZipException;
    import net.lingala.zip4j.model.ZipParameters;
    import net.lingala.zip4j.util.Zip4jConstants;

    /**
    * 压缩工具类,依赖zip4j
    *
    * @author L.X <gugia@qq.com>
    */
    public class CompressUtils {

    /**
    * 解压加密的压缩文件
    *
    * @param zipfile
    * @param dest
    * @param passwd
    * @throws ZipException
    */
    public void unZip(File zipfile, String dest, String passwd) throws ZipException {
    ZipFile zfile = new ZipFile(zipfile);
    // zfile.setFileNameCharset("GBK");//在GBK系统中需要设置
    if (!zfile.isValidZipFile()) {
    throw new ZipException("压缩文件不合法,可能已经损坏!");
    }
    File file = new File(dest);
    if (file.isDirectory() && !file.exists()) {
    file.mkdirs();
    }
    if (zfile.isEncrypted()) {
    zfile.setPassword(passwd.toCharArray());
    }
    zfile.extractAll(dest);
    }

    /**
    * 压缩文件且加密
    *
    * @param src
    * @param dest
    * @param isCreateDir
    * @param passwd
    */
    public void zip(String src, String dest, boolean isCreateDir, String passwd) {
    File srcfile = new File(src);
    //创建目标文件
    String destname = buildDestFileName(srcfile, dest);
    ZipParameters para = new ZipParameters();
    para.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
    para.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
    if (passwd != null) {
    para.setEncryptFiles(true);
    para.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);
    para.setPassword(passwd.toCharArray());
    }
    try {
    ZipFile zipfile = new ZipFile(destname);
    if (srcfile.isDirectory()) {
    if (!isCreateDir) {
    File[] listFiles = srcfile.listFiles();
    ArrayList<File> temp = new ArrayList<>();
    Collections.addAll(temp, listFiles);
    zipfile.addFiles(temp, para);
    }
    zipfile.addFolder(srcfile, para);
    } else {
    zipfile.addFile(srcfile, para);
    }
    } catch (ZipException ex) {
    System.out.println(ex.getMessage());
    }

    }

    /**
    * 目标文件名称
    *
    * @param srcfile
    * @param dest
    * @return
    */
    public String buildDestFileName(File srcfile, String dest) {
    if (dest == null) {//没有给出目标路径时
    if (srcfile.isDirectory()) {
    dest = srcfile.getParent() + File.separator + srcfile.getName() + ".zip";
    } else {
    String filename = srcfile.getName().substring(0, srcfile.getName().lastIndexOf("."));
    dest = srcfile.getParent() + File.separator + filename + ".zip";
    }
    } else {
    createPath(dest);//路径的创建
    if (dest.endsWith(File.separator)) {
    String filename;
    if (srcfile.isDirectory()) {
    filename = srcfile.getName();
    } else {
    filename = srcfile.getName().substring(0, srcfile.getName().lastIndexOf("."));
    }
    dest += filename + ".zip";
    }
    }
    return dest;
    }

    /**
    * 路径创建
    *
    * @param dest
    */
    private void createPath(String dest) {
    File destDir;
    if (dest.endsWith(File.separator)) {
    destDir = new File(dest);//给出的是路径时
    } else {
    destDir = new File(dest.substring(0, dest.lastIndexOf(File.separator)));
    }
    if (!destDir.exists()) {
    destDir.mkdirs();
    }
    }
    }

    调用

    CompressUtils compressUtils = new CompressUtils();
    compressUtils.zip(zippaths[0], zipFilePath, true, null);

  • 相关阅读:
    Apple Swift编程语言入门中文教程
    WWDC 2014 Session 208/231 CloudKit 读书笔记
    微信开源项目解说使用公开课
    winform 实现彩票功能
    c#+windows api SetWindowsHookEx 全局钩子 demo 下载
    GPS-Graph Processing System Graph Coloring算法分析 (三)
    00105_UDP和TCP协议
    雷林鹏分享:jQuery EasyUI 表单
    雷林鹏分享:jQuery EasyUI 表单
    雷林鹏分享:jQuery EasyUI 表单
  • 原文地址:https://www.cnblogs.com/niuxi/p/7161851.html
Copyright © 2020-2023  润新知