java压缩 GZIP进行简单压缩,ZIP进行多文件保存
java压缩 GZIP进行简单压缩,ZIP进行多文件保存
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGlhbmdydWkxOTg4/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
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)*/ } }
java压缩 GZIP进行简单压缩,ZIP进行多文件保存的更多相关文章
- 分享非常有用的Java程序 (关键代码) (三)---创建ZIP和JAR文件
原文:分享非常有用的Java程序 (关键代码) (三)---创建ZIP和JAR文件 import java.util.zip.*; import java.io.*; public class Zip ...
- Java压缩技术(二) ZIP压缩——Java原生实现
原文:http://snowolf.iteye.com/blog/642298 去年整理了一篇ZLib算法Java实现(Java压缩技术(一) ZLib),一直惦记却没时间补充.今天得空,整理一下ZI ...
- java nio 写一个完整的http服务器 支持文件上传 chunk传输 gzip 压缩 使用过程 和servlet差不多
java nio 写一个完整的http服务器 支持文件上传 chunk传输 gzip 压缩 也仿照着 netty处理了NIO的空轮询BUG 本项目并不复杂 代码不多 ...
- [拾 得] zip gzip bzip2 & tar 压缩/打包 四大金刚
坚持知识分享,该文章由Alopex编著, 转载请注明源地址: http://www.cnblogs.com/alopex/ 索引: 介绍压缩和打包 gzip bzip2 zip 的基本使用 gz ...
- Linux学习之CentOS(十九)------linux 下压缩与解压之 tar、gzip、bzip2、zip、rar
将文件存储到归档文件中或者从归档文件中获取原始文件,以及为文件创建归档文件 tar [option] [modifiers] [file-list] 参数 file-list是tar进行归档和提取的文 ...
- java zip压缩优化版 解决压缩后文件一直被占用无法删除
最近进行zip操作,从网上找到一个处理方法,但是经过试验存在一些bug,主要是文件流的申明存在问题,导致jvm一直占用文件而不释放,特意把自己修改的发出来,已备记录 import java.io.Bu ...
- Java压缩技术(三) ZIP解压缩——Java原生实现
原文:http://snowolf.iteye.com/blog/642492 JavaEye的朋友跟我说:“你一口气把ZIP压缩和解压缩都写到一个帖子里,我看起来很累,不如分开好阅读”.ok,面向读 ...
- JAVA调用外部安装7-Zip压缩和解压zip文件
1.首先在本地安装7-Zip(下载链接:https://www.7-zip.org/)2.调用7-Zip压缩.zip文件: /** * 生成.zip压缩文件 * @param fi ...
- Web服务器处理HTTP压缩之gzip、deflate压缩
现如今在处理http请求的时候,由于请求的资源较多,如果不启用压缩的话,那么页面请求的流量将会非常大.启用gzip压缩,在一定程度上会大大的提高页面性能. 目录 一.什么是gzip 二.什么是de ...
随机推荐
- IPython Notebook error: Error loading notebook
打开jupyter突然报错: An unknown error occurred while loading this notebook. This version can load notebook ...
- 用java实现word转html
由于项目需要,要完成将上传的word文件转成html文件的功能.在网上搜了一下,大致有3种方法:1.用jacob实现 2.用poi实现 3.用openoffice实现. 从网上来看好像jacob用的人 ...
- hdu 5172(线段树||HASH)
GTY's gay friends Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- mysql查看每张表的空间使用情况
use information_schema; /,),'MB') as data ,concat(round(index_length//,),'MB') as indexweight from T ...
- 安卓长按交互onCreateContextMenu的简单 用法
1.可在activity和fragment中使用. 2.使用方法 (1)注册 registerForContextMenu(btn);//btn是要实现交互的控件 (2)重写onCreateConte ...
- HashMap底层实现原理/HashMap与HashTable区别/HashMap与HashSet区别(转)
HashMap底层实现原理/HashMap与HashTable区别/HashMap与HashSet区别 文章来源:http://www.cnblogs.com/beatIteWeNerverGiveU ...
- 链式前向星写法下的DFS和BFS
Input 5 7 1 2 2 3 3 4 1 3 4 1 1 5 4 5 output 1 5 3 4 2 #include<bits/stdc++.h> using namespace ...
- MySQL数据库增删改字段(属性)
MySQL数据库的各种操作今天在这里总结一下: 一.增加 1.在已有的表中添加新的字段: 首先是增加表的字段,比如一张表原本没有字段“ Time ”,现在我们要增加这样一个字段,可以用下面的SQL语句 ...
- 拓扑排序(Topological Order)UVa10305 Ordering Tasks
2016/5/19 17:39:07 拓扑排序,是对有向无环图(Directed Acylic Graph , DAG )进行的一种操作,这种操作是将DAG中的所有顶点排成一个线性序列,使得图中的任意 ...
- hdu6121
hdu6121 题意 给出一棵树,\(0\) 为根节点,节点 \(i\) 的父节点标号是 \(\lfloor\frac{i-1}{k}\rfloor\),求所有子树大小的异或和. 分析 找规律.在纸上 ...