Java中Zip进行多文件保存
来源:互联网 发布:可视化数据设计 编辑:程序博客网 时间:2024/06/06 04:20
<pre name="code" class="java">package com.io.zip;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.Enumeration;import java.util.zip.Adler32;import java.util.zip.CheckedInputStream;import java.util.zip.ZipEntry;import java.util.zip.ZipFile;import java.util.zip.ZipInputStream;public class DeZipFile {public static void main(String[] args) throws Exception {String filepath = "F:\\z321.zip";String destpath = "F:\\dezip";// test1(filepath);test2(filepath, destpath);}/** * 查看压缩包下面有哪些文件 * * @param filepath * @throws IOException */private static void test1(String filepath) throws IOException {ZipFile zf = new ZipFile(filepath);Enumeration enums = zf.entries();while (enums.hasMoreElements()) {ZipEntry entry = (ZipEntry) enums.nextElement();System.out.println("文件:" + entry.getName());}}/** * 解压filepath下面的文件到destpath路径下面 * * @param filepath * @param destpath * @throws IOException */private static void test2(String filepath, String destpath)throws IOException {File d = new File(destpath);if(!d.exists()){d.mkdirs();}FileInputStream fis = new FileInputStream(filepath);CheckedInputStream cis = new CheckedInputStream(fis, new Adler32());ZipInputStream zis = new ZipInputStream(cis);FileOutputStream out;ZipEntry ze;while ((ze = zis.getNextEntry()) != null) {out = new FileOutputStream(destpath + File.separator + ze.getName());int x;while ((x = zis.read()) != -1) {out.write(x);}out.close();}zis.close();}}
package com.io.zip;import java.io.BufferedOutputStream;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FilenameFilter;import java.io.IOException;import java.nio.charset.Charset;import java.util.regex.Pattern;import java.util.zip.Adler32;import java.util.zip.CRC32;import java.util.zip.CheckedOutputStream;import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;public class ZipManyFile {public static void main(String[] args) throws Exception {// String file1 = "E:\\cookie.java",file2 = "F:\\core.log",file3 = "E:\\wold.png";// zipfiles(file1,file2,file3);//压缩整个文件下的文件String filedir = "F:\\testzip";zipfiles2(filedir);}<span style="white-space:pre"> </span>
private static void zipfiles(String... files) throws IOException {FileOutputStream fos = new FileOutputStream("F:\\z321.zip");CheckedOutputStream cos = new CheckedOutputStream(fos, new CRC32());ZipOutputStream zos = new ZipOutputStream(cos);zos.setComment("z321test测试");for (String fileName : files) {File file = new File(fileName);FileInputStream fis = new FileInputStream(file);zos.putNextEntry(new ZipEntry(file.getName()));int x;while ((x = fis.read()) != -1) {zos.write(x);}fis.close();}zos.close();System.out.println("checksum: " + cos.getChecksum().getValue());}private static void zipfiles2(String filedir) throws IOException {FileOutputStream fos = new FileOutputStream("F:\\some3333.zip");CheckedOutputStream cos = new CheckedOutputStream(fos, new Adler32());ZipOutputStream zos = new ZipOutputStream(cos, Charset.forName("gb2312"));zos.setComment("测试压缩一个文件下的所有文件haha");BufferedOutputStream bos = new BufferedOutputStream(zos);final String reg = "[hH].*";File file = new File(filedir);File[] fs = file.listFiles(new FilenameFilter() {private Pattern pattern = Pattern.compile(reg);@Overridepublic boolean accept(File dir, String name) {return pattern.matcher(name).matches();}});for(File f : fs){//FileInputStream fis = new FileInputStream(f);BufferedReader br = new BufferedReader(new FileReader(f));zos.putNextEntry(new ZipEntry(file.getName()+"\\"+f.getName()));int x;while((x=br.read())!=-1){bos.write(x);}br.close();}bos.close();System.out.println("checksum: "+cos.getChecksum().getValue());}}
</pre><pre name="code" class="java">
0 0
- Java中Zip进行多文件保存
- Java用ZIP进行多文件保存
- Java 使用Zip进行多文件保存和读取
- Java用ZIP进行多文件压缩保存
- Java中的Zip进行多文件的保存
- 用 Zip 进行多文件保存
- 用ZIP进行多文件保存
- java压缩 GZIP进行简单压缩,ZIP进行多文件保存
- 多文件进行zip压缩
- Java 中 解压缩文件 ZIP
- 用zip压缩保存文件
- 在java中解压缩一个zip文件
- 在Java中操作Zip文件
- java中生成和解压缩zip文件
- java 从zip包中复制文件
- 在 Java 中解压 zip 文件
- java 将多个文件打包成一个zip后进行下载
- java中利用jxl包对Excel文件进行修改保存
- XML解析
- android 线程池的应用
- HQL当前时间与Mysql数据库时间比较
- : Exception sending context initialized event to listener instance of class org.springframework.web.
- POJ1423————Big Number
- Java中Zip进行多文件保存
- 通过反汇编一个简单的C程序,分析汇编代码理解计算机是如何工作的(原创)
- Fiddler调试线上JS代码
- ios侧滑菜单使用经验
- 高精度运算
- HDU 1242 Rescue
- (七) Library Projects(库项目)
- windows下memcached的安装与使用
- android学习中