java 多文件打包压缩

Wesley13
• 阅读 664
public static void main(String[] args) {
        File[] srcFiles = { new File("E:\\testZip\\testZip.zip"), new File("E:\\testZip\\test2.json"), new File("E:\\testZip\\test3.txt") };
        File zipFile = new File("E:\\ZipFile.zip");
        // 调用压缩方法
        zipFiles(srcFiles, zipFile);
    }
    public static void zipFiles(File[] srcFiles, File zipFile) {
        // 判断压缩后的文件存在不,不存在则创建
        if (!zipFile.exists()) {
            try {
                zipFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 创建 FileOutputStream 对象
        FileOutputStream fileOutputStream = null;
        // 创建 ZipOutputStream
        ZipOutputStream zipOutputStream = null;
        // 创建 FileInputStream 对象
        FileInputStream fileInputStream = null;

        try {
            // 实例化 FileOutputStream 对象
            fileOutputStream = new FileOutputStream(zipFile);
            // 实例化 ZipOutputStream 对象
            zipOutputStream = new ZipOutputStream(fileOutputStream);
            // 创建 ZipEntry 对象
            ZipEntry zipEntry = null;
            // 遍历源文件数组
            for (int i = 0; i < srcFiles.length; i++) {
                // 将源文件数组中的当前文件读入 FileInputStream 流中
                fileInputStream = new FileInputStream(srcFiles[i]);
                // 实例化 ZipEntry 对象,源文件数组中的当前文件
                zipEntry = new ZipEntry(srcFiles[i].getName());
                zipOutputStream.putNextEntry(zipEntry);
                // 该变量记录每次真正读的字节个数
                int len;
                // 定义每次读取的字节数组
                byte[] buffer = new byte[1024];
                while ((len = fileInputStream.read(buffer)) > 0) {
                    zipOutputStream.write(buffer, 0, len);
                }
            }
            zipOutputStream.closeEntry();
            zipOutputStream.close();
            fileInputStream.close();
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
点赞
收藏
评论区
推荐文章
Wesley13 Wesley13
2年前
java.net.ConnectException:Connection timed out:connect 出现连接超时的问题?
今天在eclipse中连接Linux上的MySQL出现了java.net.ConnectException:Connectiontimedout,具体如下图:!(https://oscimg.oschina.net/oscnet/7e0f35e44f3623da2d108249e4ff9e2195e.png) 我猜想可能是因为防火墙没关导
待兔 待兔
2星期前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
红烧土豆泥 红烧土豆泥
3年前
Java的文件压缩-Zip格式
language/文件压缩/publicclassZipFileCompresspublicstaticvoidmain(Stringargs)throwsIOExceptionFilefilenewFile("E:\\aaa\\web01");StringsavePath
Wesley13 Wesley13
2年前
javase IO
File类,用来表示一个文件或者一个文件夹,通过File类的对象来对文件夹的名字,路径,大小等等访问,但是不可以直接访问文件内的数据构造newFile(Stringurl)指定路径newFile(StringparentUrl,Stringurl)指定父路径,当前文件newFile(Fileparent,
Wesley13 Wesley13
2年前
java +
publicclassSample{publicstaticvoidmain(Stringargs){inta,b,c,d,e;ScannersnewScanner(System.in);System.out.print("Ente
Wesley13 Wesley13
2年前
Java一种减少图片存储空间的方法(转换成jpg格式)
  主要用到BufferedImage对象,将原始图片保存为jpg格式:publicstaticvoidthumbImage(Stringinput,StringnewFile)throwsException{Imagesrcjavax.imageio.ImageIO.read(newFile(inpu
Stella981 Stella981
2年前
Android So动态加载 优雅实现与原理分析
背景:漫品Android客户端集成适配转换功能(基于目标识别(So库35M)和人脸识别库(5M)),导致apk体积50M左右,为优化客户端体验,决定实现So文件动态加载.!(https://oscimg.oschina.net/oscnet/00d1ff90e4b34869664fef59e3ec3fdd20b.png)点击上方“蓝字”关注我
Wesley13 Wesley13
2年前
Java 9 被无情抛弃,Java 8 直接升级到 Java 10!!
!(https://oscimg.oschina.net/oscnet/3e08a942dd884e9ab82b63a1f3c4aada.jpg"未命名文件.jpg")Java技术栈不可错过的Java 技术公众号!(https://oscimg.oschina.net/oscnet/00fcff52518e
Wesley13 Wesley13
2年前
Java三大特性
_Java面向对象编程三大特性:封装继承多态(https://snailclimb.gitee.io/javaguide//docs/java/Java%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86?id_11java%e9%9d%a2%e5%90%91%e5%af%b9%e8%b1%a1%e7%bc%96%e
Wesley13 Wesley13
2年前
unity将 -u4E00 这种 编码 转汉字 方法
 unity中直接使用 JsonMapper.ToJson(对象),取到的字符串,里面汉字可能是\\u4E00类似这种其实也不用转,服务器会通过类似fastjson发序列化的方式,将json转对象,获取对象的值就是中文但是有时服务器要求将传参中字符串中类似\\u4E00这种转汉字,就需要下面 publ