|
| 1 | +package org.javacore.io.zip; |
| 2 | + |
| 3 | +import java.io.BufferedInputStream; |
| 4 | +import java.io.BufferedOutputStream; |
| 5 | +import java.io.BufferedReader; |
| 6 | +import java.io.File; |
| 7 | +import java.io.FileInputStream; |
| 8 | +import java.io.FileNotFoundException; |
| 9 | +import java.io.FileOutputStream; |
| 10 | +import java.io.FileReader; |
| 11 | +import java.io.IOException; |
| 12 | +import java.util.Enumeration; |
| 13 | +import java.util.zip.Adler32; |
| 14 | +import java.util.zip.CheckedInputStream; |
| 15 | +import java.util.zip.CheckedOutputStream; |
| 16 | +import java.util.zip.ZipEntry; |
| 17 | +import java.util.zip.ZipFile; |
| 18 | +import java.util.zip.ZipInputStream; |
| 19 | +import java.util.zip.ZipOutputStream; |
| 20 | + |
| 21 | +/* |
| 22 | + * Copyright [2015] [Jeff Lee] |
| 23 | + * |
| 24 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 25 | + * you may not use this file except in compliance with the License. |
| 26 | + * You may obtain a copy of the License at |
| 27 | + * |
| 28 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 29 | + * |
| 30 | + * Unless required by applicable law or agreed to in writing, software |
| 31 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 32 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 33 | + * See the License for the specific language governing permissions and |
| 34 | + * limitations under the License. |
| 35 | + */ |
| 36 | + |
| 37 | +/** |
| 38 | + * @author Jeff Lee |
| 39 | + * @since 2015-10-17 14:58:59 |
| 40 | + * 利用Zip进行多文件保存 |
| 41 | + */ |
| 42 | +public class ZipCompress { |
| 43 | + private static String filePath = "src" + File.separator + |
| 44 | + "org" + File.separator + |
| 45 | + "javacore" + File.separator + |
| 46 | + "io" + File.separator; |
| 47 | + |
| 48 | + private static String[] fileNames= new String[] { |
| 49 | + filePath + "BufferedInputFileT.java", |
| 50 | + filePath + "ChangeSystemOut.java" |
| 51 | + }; |
| 52 | + |
| 53 | + public static void main(String[] args) throws IOException { |
| 54 | + zipFiles(fileNames); |
| 55 | + } |
| 56 | + |
| 57 | + private static void zipFiles(String[] fileNames) |
| 58 | + throws IOException { |
| 59 | + // 获取zip文件输出流 |
| 60 | + FileOutputStream f = new FileOutputStream("test.zip"); |
| 61 | + // 从文件输出流中获取数据校验和输出流,并设置Adler32 |
| 62 | + CheckedOutputStream csum = new CheckedOutputStream(f,new Adler32()); |
| 63 | + // 从数据校验和输出流中获取Zip输出流 |
| 64 | + ZipOutputStream zos = new ZipOutputStream(csum); |
| 65 | + // 从Zip输出流中获取缓冲输出流 |
| 66 | + BufferedOutputStream out = new BufferedOutputStream(zos); |
| 67 | + // 设置Zip文件注释 |
| 68 | + zos.setComment("测试 java zip stream"); |
| 69 | + for (String file : fileNames) { |
| 70 | + System.out.println("写入文件: " + file); |
| 71 | + // 获取文件输入字符流 |
| 72 | + BufferedReader in = |
| 73 | + new BufferedReader(new FileReader(file)); |
| 74 | + // 想Zip处理写入新的文件条目,并流定位到数据开始处 |
| 75 | + zos.putNextEntry(new ZipEntry(file)); |
| 76 | + int c; |
| 77 | + while ((c = in.read()) > 0) |
| 78 | + out.write(c); |
| 79 | + in.close(); |
| 80 | + // 刷新Zip输出流,将缓冲的流写入该流 |
| 81 | + out.flush(); |
| 82 | + } |
| 83 | + // 文件全部写入Zip输出流后,关闭 |
| 84 | + out.close(); |
| 85 | + |
| 86 | + // 输出数据校验和 |
| 87 | + System.out.println("数据校验和: " + csum.getChecksum().getValue()); |
| 88 | + System.out.println("读取zip文件"); |
| 89 | + // 读取test.zip文件输入流 |
| 90 | + FileInputStream fi = new FileInputStream("test.zip"); |
| 91 | + // 从文件输入流中获取数据校验和流 |
| 92 | + CheckedInputStream csumi = new CheckedInputStream(fi,new Adler32()); |
| 93 | + // 从数据校验和流中获取Zip解压流 |
| 94 | + ZipInputStream in2 = new ZipInputStream(csumi); |
| 95 | + // 从Zip解压流中获取缓冲输入流 |
| 96 | + BufferedInputStream bis = new BufferedInputStream(in2); |
| 97 | + // 创建文件条目 |
| 98 | + ZipEntry zipEntry; |
| 99 | + while ((zipEntry = in2.getNextEntry()) != null) { |
| 100 | + System.out.println("读取文件: " + zipEntry); |
| 101 | + int x; |
| 102 | + while ((x = bis.read()) > 0) |
| 103 | + System.out.write(x); |
| 104 | + } |
| 105 | + if (fileNames.length == 1) |
| 106 | + System.out.println("数据校验和: " + csumi.getChecksum().getValue()); |
| 107 | + bis.close(); |
| 108 | + |
| 109 | + // 获取Zip文件 |
| 110 | + ZipFile zf = new ZipFile("test.zip"); |
| 111 | + // 获取文件条目枚举 |
| 112 | + Enumeration e = zf.entries(); |
| 113 | + while (e.hasMoreElements()) { |
| 114 | + // 从Zip文件的枚举中获取文件条目 |
| 115 | + ZipEntry ze2 = (ZipEntry) e.nextElement(); |
| 116 | + System.out.println("文件: " + ze2); |
| 117 | + } |
| 118 | + |
| 119 | + } |
| 120 | +} |
0 commit comments