当前位置: 首页 > news >正文

java压缩 GZIP进行简单压缩,ZIP进行多文件保存

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

java压缩  GZIP进行简单压缩,ZIP进行多文件保存


package org.rui.io.compress;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
/**
 * 用GZIP进行简单压缩
 * @author lenovo
 *
 */
public class GZIPcompress {
	static String path="D:\\Users\\liangrui\\workspace\\thinking\\src\\org\\rui\\io\\compress\\";
	static String [] arg=new String[]{path+"GZIPcompress.java"};
	public static void main(String[] args) throws Exception {
		//in
		BufferedReader in=new BufferedReader(new FileReader(arg[0]));
		//out
		BufferedOutputStream out=new BufferedOutputStream(new GZIPOutputStream(
				new FileOutputStream("test.gz")
				));
		System.out.println("writing file");
		int c;
		while((c=in.read())!=-1)
		{
			out.write(c);
		}
		//close
		in.close();
		out.close();
		System.out.println("reading file================");
		BufferedReader br=new BufferedReader(
				new InputStreamReader(
						new GZIPInputStream(
								new FileInputStream("test.gz")
								)
						)
				);
		
		String s;
		while((s=br.readLine())!=null)
			System.out.println(s);
	}

}




package org.rui.io.compress;

import java.io.BufferedInputStream;
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.util.Enumeration;
import java.util.zip.Adler32;
import java.util.zip.CheckedInputStream;
import java.util.zip.CheckedOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

/**
 * 用Zip进行多文件保存
 * @author lenovo
 *
 */
public class ZipCompress {
	//static String path="D:\\Users\\liangrui\\workspace\\thinking\\src\\org\\rui\\io\\compress\\";
	static String path=new File("").getAbsolutePath()+"\\src\\org\\rui\\io\\compress\\";
	static String [] arg=new String[]{path+"GZIPcompress.java",path+"ZipCompress.java"};
	
	public static void main(String[] args) throws Exception {
		//out
		FileOutputStream f=new FileOutputStream("test.zip");
		CheckedOutputStream cos=new CheckedOutputStream(f,new Adler32());
		ZipOutputStream zos=new ZipOutputStream(cos);
		//out
		BufferedOutputStream out=new BufferedOutputStream(zos);
		zos.setComment("A test of java zipping");
		
		for(String s:arg)
		{
			System.out.println("writing file "+s);
			BufferedReader in =new BufferedReader(new FileReader(s));
			//
			zos.putNextEntry(new ZipEntry(s));
			int c;
			while((c=in.read())!=-1){
				out.write(c);
			}
			in.close();
			out.flush();
		}
		
		out.close();
		//checksum valid only after the file has been closed!
		System.out.println("reading file================");
		FileInputStream fi=new FileInputStream("test.zip");
		CheckedInputStream csumi=new CheckedInputStream(fi,new Adler32());
		ZipInputStream zis=new ZipInputStream(csumi);
		BufferedInputStream bis=new BufferedInputStream(zis);
		ZipEntry ze;
		while((ze=zis.getNextEntry())!=null)
		{
			System.out.println("Reading file "+ze);
			int x;
			while((x=bis.read())!=-1)
			{
				//System.out.print((char)x);
				System.out.write(x);
			}
		}
		
		//
		if(arg.length==1)
			System.out.println("checksum:"+csumi.getChecksum().getValue());
		bis.close();
		//alternative way to open and read zip files
		ZipFile zf=new ZipFile("test.zip");
		
		Enumeration e=zf.entries();
		while(e.hasMoreElements())
		{
			ZipEntry ze2=(ZipEntry) e.nextElement();
			System.out.println("file:"+ze2);
			//...and extract the data as before
		}
		/*if(arg.length==1)*/
		
	}

}












转载于:https://my.oschina.net/pangzhuzhu/blog/301788

相关文章:

  • mongoDB 文档操作_增
  • 我的开源项目:FLV封装格式分析器
  • 【Leetcode】104. 二叉树的最大深度
  • SDI,ASI,HDMI,DP等接口的区别
  • Luogu P3181 [HAOI2016]找相同字符 广义$SAM$
  • 主流视音频平台参数
  • python中的with语句
  • LAV Filter 源代码分析 4: LAV Video (2)
  • CSS:z-index
  • RDS-Mysql 物理备份恢复到本地数据库上
  • 客户端网络库实现真的很简单吗?
  • HTPC家庭娱乐和XBOX未来发展畅想另:创业工作机会
  • Socket IO与NIO(四)
  • 【算法导论】学习笔记——第6章 堆排序
  • 转:网络协议概览
  • 网络传输文件的问题
  • 【附node操作实例】redis简明入门系列—字符串类型
  • Fastjson的基本使用方法大全
  • in typeof instanceof ===这些运算符有什么作用
  • LeetCode541. Reverse String II -- 按步长反转字符串
  • Logstash 参考指南(目录)
  • Synchronized 关键字使用、底层原理、JDK1.6 之后的底层优化以及 和ReenTrantLock 的对比...
  • Yeoman_Bower_Grunt
  • 从0搭建SpringBoot的HelloWorld -- Java版本
  • 服务器从安装到部署全过程(二)
  • 工程优化暨babel升级小记
  • 前端攻城师
  • 一些css基础学习笔记
  • 《码出高效》学习笔记与书中错误记录
  • ​ssh-keyscan命令--Linux命令应用大词典729个命令解读
  • !! 2.对十份论文和报告中的关于OpenCV和Android NDK开发的总结
  • !$boo在php中什么意思,php前戏
  • # Swust 12th acm 邀请赛# [ K ] 三角形判定 [题解]
  • # 达梦数据库知识点
  • #android不同版本废弃api,新api。
  • (4)(4.6) Triducer
  • (9)YOLO-Pose:使用对象关键点相似性损失增强多人姿态估计的增强版YOLO
  • (cljs/run-at (JSVM. :browser) 搭建刚好可用的开发环境!)
  • (Matalb分类预测)GA-BP遗传算法优化BP神经网络的多维分类预测
  • (Matalb时序预测)PSO-BP粒子群算法优化BP神经网络的多维时序回归预测
  • (二) Windows 下 Sublime Text 3 安装离线插件 Anaconda
  • (附源码)springboot建达集团公司平台 毕业设计 141538
  • (六)vue-router+UI组件库
  • (十八)devops持续集成开发——使用docker安装部署jenkins流水线服务
  • (一)kafka实战——kafka源码编译启动
  • (译)计算距离、方位和更多经纬度之间的点
  • (原创)boost.property_tree解析xml的帮助类以及中文解析问题的解决
  • (转) Face-Resources
  • (转)linux下的时间函数使用
  • (转)Scala的“=”符号简介
  • .bat文件调用java类的main方法
  • .Net core 6.0 升8.0
  • .NET Framework 服务实现监控可观测性最佳实践
  • .NET WebClient 类下载部分文件会错误?可能是解压缩的锅
  • .Net 中Partitioner static与dynamic的性能对比