• 使用 Java 将多个文件压缩成一个压缩文件


    使用 Java 将多个文件压缩成一个压缩文件

    一、内容

      ①使用 Java 将多个文件打包压缩成一个压缩文件;

      ②主要使用 java.io 下的类

    二、源代码:ZipMultiFile.java

    复制代码
     1 package cn.com.zfc.day018;
     2 
     3 import java.io.File;
     4 import java.io.FileInputStream;
     5 import java.io.FileOutputStream;
     6 import java.io.IOException;
     7 import java.util.zip.ZipEntry;
     8 import java.util.zip.ZipOutputStream;
     9 
    10 /**
    11  * @describe 压缩多个文件
    12  * @author zfc
    13  * @date 2018年1月11日 下午8:34:00
    14  */
    15 public class ZipMultiFile {
    16 
    17     public static void main(String[] args) {
    18         File[] srcFiles = { new File("E:\母亲.mp4"), new File("E:\飞机.pdf"), new File("E:\家里密码.txt") };
    19         File zipFile = new File("E:\ZipFile.zip");
    20         // 调用压缩方法
    21         zipFiles(srcFiles, zipFile);
    22     }
    23 
    24     public static void zipFiles(File[] srcFiles, File zipFile) {
    25         // 判断压缩后的文件存在不,不存在则创建
    26         if (!zipFile.exists()) {
    27             try {
    28                 zipFile.createNewFile();
    29             } catch (IOException e) {
    30                 e.printStackTrace();
    31             }
    32         }
    33         // 创建 FileOutputStream 对象
    34         FileOutputStream fileOutputStream = null;
    35         // 创建 ZipOutputStream
    36         ZipOutputStream zipOutputStream = null;
    37         // 创建 FileInputStream 对象
    38         FileInputStream fileInputStream = null;
    39 
    40         try {
    41             // 实例化 FileOutputStream 对象
    42             fileOutputStream = new FileOutputStream(zipFile);
    43             // 实例化 ZipOutputStream 对象
    44             zipOutputStream = new ZipOutputStream(fileOutputStream);
    45             // 创建 ZipEntry 对象
    46             ZipEntry zipEntry = null;
    47             // 遍历源文件数组
    48             for (int i = 0; i < srcFiles.length; i++) {
    49                 // 将源文件数组中的当前文件读入 FileInputStream 流中
    50                 fileInputStream = new FileInputStream(srcFiles[i]);
    51                 // 实例化 ZipEntry 对象,源文件数组中的当前文件
    52                 zipEntry = new ZipEntry(srcFiles[i].getName());
    53                 zipOutputStream.putNextEntry(zipEntry);
    54                 // 该变量记录每次真正读的字节个数
    55                 int len;
    56                 // 定义每次读取的字节数组
    57                 byte[] buffer = new byte[1024];
    58                 while ((len = fileInputStream.read(buffer)) > 0) {
    59                     zipOutputStream.write(buffer, 0, len);
    60                 }
    61             }
    62             zipOutputStream.closeEntry();
    63             zipOutputStream.close();
    64             fileInputStream.close();
    65             fileOutputStream.close();
    66         } catch (IOException e) {
    67             e.printStackTrace();
    68         }
    69 
    70     }
    71 }
    复制代码

     

    三、运行结果

  • 相关阅读:
    图像的余弦变换
    图像的傅里叶变换
    图像的频域变换
    数字图像处理基本运算
    数字图像处理基本概念
    推荐几款提升效率的神器
    机器学习中常用的距离及其python实现
    打造自己的图像识别模型
    使用Colaboratory的免费GPU训练神经网络
    学霸修炼的秘籍
  • 原文地址:https://www.cnblogs.com/jpfss/p/9830594.html
Copyright © 2020-2023  润新知