Java编程思想:压缩
2024-09-01 01:39:11
import java.io.*; import java.util.Enumeration; import java.util.zip.*; public class Test { public static void main(String[] args){ // GZIPCompress.test(); ZIPCompress.test(); } } class GZIPCompress { public static void test() { try { //创建一个字符输入流 BufferedReader in = new BufferedReader(new FileReader("temp.tmp")); //创建一个压缩输出流 BufferedOutputStream out = new BufferedOutputStream( new GZIPOutputStream( new FileOutputStream("temp1.tmp"))); System.out.println("Writing File..."); int c; while ((c = in.read()) != -1) { out.write(c); } in.close(); out.close(); System.out.println("Reading File..."); BufferedReader in2 = new BufferedReader( new InputStreamReader( new GZIPInputStream( new FileInputStream("temp1.tmp")))); String s; while ((s = in2.readLine()) != null) { System.out.println(s); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } class ZIPCompress { public static void test() { String[] args = new String[]{ "temp.tmp", "temp1.tmp", "temp2.tmp", "temp3.tmp", "temp4.tmp", }; try { //进行压缩 FileOutputStream f = new FileOutputStream("test.zip"); CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32()); ZipOutputStream zos = new ZipOutputStream(csum); BufferedOutputStream out = new BufferedOutputStream(zos); zos.setComment("A Test Of Java Zipping..."); for (String arg : args) { System.out.println("Writing file: " + arg); BufferedReader in = new BufferedReader(new FileReader(arg)); zos.putNextEntry(new ZipEntry(arg)); int c; while ((c = in.read()) != -1) { out.write(c); } in.close(); out.flush(); } out.close(); System.out.println("Checksum:"+csum.getChecksum().getValue()); //进行解压 /* System.out.println("Reading File..."); FileInputStream fi = new FileInputStream("test.zip"); CheckedInputStream csumi = new CheckedInputStream(fi, new Adler32()); ZipInputStream in = new ZipInputStream(csumi); BufferedInputStream bis = new BufferedInputStream(in); ZipEntry ze; while ((ze = in.getNextEntry()) != null) { System.out.println("Reading File: " + ze); int x; while ((x = bis.read()) != -1) { System.out.println(x); } } bis.close(); */ //查看压缩文件的目录 ZipFile zf = new ZipFile("test.zip"); Enumeration e = zf.entries(); while (e.hasMoreElements()) { ZipEntry ze2 = (ZipEntry)e.nextElement(); System.out.println("File: "+ze2); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
最新文章
- APIPA(Automatic Private IP Addressing,自动专用IP寻址)
- ListView设置headerview和footerview
- java 24 - 9 GUI 之 给窗体换图标、设置启动在屏幕中间、更换皮肤
- 黑马程序员:Java编程_多线程
- Java GC系列(4):垃圾回收监视和分析
- 安装webmin
- outlook圆角table
- 另类方法解决设计Web页面出现:Error Creating Control
- npm scripts 助力前端开发,实时刷新浏览器
- gc内存回收机制
- jquery倒计时(仿团购)转至 http://justcoding.iteye.com/blog/2210962
- QT 报错:Project ERROR: Xcode not set up properly. You may need to confirm the license agreement by running /usr/bin/xcodebuild.
- “一切都是消息”--MSF(消息服务框架)之【请求-响应】模式
- 机器学习(2) - KNN识别MNIST
- 第36节:Java当中的线程
- github for Mac 教程
- ACM退役前2个月总结
- 警告:Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
- [leetcode]632. Smallest Range最小范围
- 【wepy实战】wepy搭建完整项目
热门文章
- Android零基础入门第81节:Activity数据传递
- Qt在各平台上的搭建qt-everywhere(Qt for windows7-64bit, Ubuntu 12.04-32bit, 嵌入式x86平台, 嵌入式arm平台)
- Qt加载百度离线地图
- 使用C++还是QML(QML容易使用和维护,效果好)
- 系列教程 - java web开发
- 一步一步教你用IntelliJ IDEA 搭建SSM框架(3)——实现用户登录功能
- Jmeter 如何把数据库的数据依次获取作为参数传入下一个请求?附栗子
- spring cloud 系列第8篇 —— config+bus 分布式配置中心与配置热刷新 (F版本)
- 你竟然没用 Maven 构建项目?
- ssm中mapper注入失败的传奇经历