• 将某文件夹下的文件压缩成zip


    /**
    * 将某文件夹下的文件压缩成zip文件
    *
    * @param filePath 将要被压缩的文件夹 形如 xxxxxx.txt 或xxxxxx.zip等
     * @param zipOut   zip文件输出流
    * @throws IOException
    */
    public static void fileToZip(String filePath, ZipOutputStream zipOut){
    FileInputStream fileInput = null;
    BufferedInputStream bufferStream = null;
    try {
    // 需要压缩的文件
    File file = new File(filePath);
    // 获取文件名称,如果有特殊命名需求,可以将参数列表拓展,传fileName
    String fileName = file.getName();
    fileInput = new FileInputStream(filePath);
    // 缓冲
    byte[] bufferArea = new byte[1024 * 10];
    bufferStream = new BufferedInputStream(fileInput, 1024 * 10);
    // 将当前文件作为一个zip实体写入压缩流,fileName代表压缩文件中的文件名称
    zipOut.putNextEntry(new ZipEntry(fileName));
    int length = 0;
    // 最常规IO操作,不必紧张
    while ((length = bufferStream.read(bufferArea, 0, 1024 * 10)) != -1) {
    zipOut.write(bufferArea, 0, length);
    }
    // 压缩流不必关闭,使用完后再关
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    try {
    //关闭流
    fileInput.close();
    // 需要注意的是缓冲流必须要关闭流,否则输出无效
    bufferStream.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }

    其中
    zipOut = new ZipOutputStream(new FileOutputStream("d:xxxxxx.txt"));
    //将2个压缩包合并成一个
    zipOut = new ZipOutputStream(new FileOutputStream(zipFilePath));
    ZipUtils.fileToZip(zipRealtionFilePath, zipOut);
    ZipUtils.fileToZip(sourceZipPath, zipOut);
    linux下的docker操作命令及异常
  • 相关阅读:
    学习good taste代码
    linux程序运行浅析
    菜鸟安装 CocoaPods
    菜鸟安装 CocoaPods
    CocoaPods一个Objective-C第三方库的管理利器
    CocoaPods一个Objective-C第三方库的管理利器
    intrinsicContentSize和Content Hugging Priority
    intrinsicContentSize和Content Hugging Priority
    UITableViewCell delete button 上有其它覆盖层
    UITableViewCell delete button 上有其它覆盖层
  • 原文地址:https://www.cnblogs.com/ketoli/p/15015504.html
Copyright © 2020-2023  润新知