java 压缩文件夹成zip格式
星期五, 2016-11-18 | Author: Lee | JAVA-and-J2EE | 3,800 views
想用java 自动打包文件,由此来的此代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | package com.i5a6.util; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.zip.CRC32; import java.util.zip.CheckedOutputStream; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; /** * Copyright © 2016 i5a6.com. All rights reserved. * * @author: Lee * @date: 2016年11月18日 上午9:49:23 */ public class PublicDevTo { private static File zipFile; static final int BUFFER = 8192; public static void main(String[] args) throws Exception { zipFile = new File("d:/zip/i5a6.zip"); String path = "D:/public-do/i5a6/"; compress(path); } public static void compress(String... pathName) { ZipOutputStream out = null; try { FileOutputStream fileOutputStream = new FileOutputStream(zipFile); CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream, new CRC32()); out = new ZipOutputStream(cos); String basedir = ""; for (int i = 0; i < pathName.length; i++) { compress(new File(pathName[i]), out, basedir); } out.close(); } catch (Exception e) { throw new RuntimeException(e); } } public static void compress(String srcPathName) { File file = new File(srcPathName); if (!file.exists()) throw new RuntimeException(srcPathName + "不存在!"); try { FileOutputStream fileOutputStream = new FileOutputStream(zipFile); CheckedOutputStream cos = new CheckedOutputStream(fileOutputStream, new CRC32()); ZipOutputStream out = new ZipOutputStream(cos); String basedir = ""; compress(file, out, basedir); out.close(); } catch (Exception e) { throw new RuntimeException(e); } } private static void compress(File file, ZipOutputStream out, String basedir) { /* 判断是目录还是文件 */ if (file.isDirectory()) { System.out.println("压缩:" + basedir + file.getName()); compressDirectory(file, out, basedir); } else { System.out.println("压缩:" + basedir + file.getName()); compressFile(file, out, basedir); } } /** 压缩一个目录 */ private static void compressDirectory(File dir, ZipOutputStream out, String basedir) { if (!dir.exists()) return; File[] files = dir.listFiles(); for (int i = 0; i < files.length; i++) { /* 递归 */ compress(files[i], out, basedir + dir.getName() + "/"); } } /** 压缩一个文件 */ private static void compressFile(File file, ZipOutputStream out, String basedir) { if (!file.exists()) { return; } try { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); ZipEntry entry = new ZipEntry(basedir + file.getName()); out.putNextEntry(entry); int count; byte data[] = new byte[BUFFER]; while ((count = bis.read(data, 0, BUFFER)) != -1) { out.write(data, 0, count); } bis.close(); } catch (Exception e) { throw new RuntimeException(e); } } } |
更多的封装自己调整
文章作者: Lee
本文地址: https://www.pomelolee.com/1634.html
除非注明,Pomelo Lee文章均为原创,转载请以链接形式标明本文地址
No comments yet.
Leave a comment
Search
相关文章
热门文章
最新文章
文章分类
- ajax (10)
- algorithm-learn (3)
- Android (6)
- as (3)
- computer (85)
- Database (30)
- disucz (4)
- enterprise (1)
- erlang (2)
- flash (5)
- golang (3)
- html5 (18)
- ios (4)
- JAVA-and-J2EE (186)
- linux (143)
- mac (10)
- movie-music (11)
- pagemaker (36)
- php (50)
- spring-boot (2)
- Synology群晖 (2)
- Uncategorized (6)
- unity (1)
- webgame (15)
- wordpress (33)
- work-other (2)
- 低代码 (1)
- 体味生活 (40)
- 前端 (21)
- 大数据 (8)
- 游戏开发 (9)
- 爱上海 (19)
- 读书 (4)
- 软件 (3)