import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import net.sf.json.JSONObject;
public class ZipServer {
public static void main(String[] args) {
try {
ServerSocket ss=new ServerSocket(2356);
Socket s=ss.accept();
System.out.println("accept");
OutputStream os=s.getOutputStream();
DataOutputStream dos=new DataOutputStream(os);
StringBuilder sb = new StringBuilder();
JSONObject job = new JSONObject();
String str = "如果子类方法覆盖了父类方法,那么只执行子类方法,但在确定执行顺序时还是把它看作父类方法" ;
for(int i = 0; i < 300; i++) {
job.put("list"+i, str);
}
//压缩job
ByteArrayOutputStream out = new ByteArrayOutputStream();
ZipOutputStream zout = new ZipOutputStream(out);
zout.putNextEntry(new ZipEntry("0"));
zout.write(job.toString().getBytes());
zout.closeEntry();
byte[] compressed = out.toByteArray();
dos.writeInt(compressed.length);
dos.write(compressed);
dos.flush();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.Socket;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import net.sf.json.JSONObject;
public class ZipClient {
public static void main(String[] args) {
try {
Socket socket=new Socket("127.0.0.1",2356);
InputStream in = socket.getInputStream();
byte[] length = new byte[4];
int lengthOff = -1;
//获取解压byte[]长度
do {
++lengthOff;
if (-1 == in.read(length, lengthOff, 1)) {
}
}while (lengthOff != 3);
int length = b2i(length);
//接收压缩文件
byte[] buff = new byte[length];
int receivedLength = 0;
while (receivedLength < length) {
int r = in.read(buff, receivedLength, length - receivedLength);
if (-1 == r) {
System.out.println("通信错误:接收内容失败");
}
receivedLength += r;
}
//解压byte[]数据
ByteArrayInputStream inS = new ByteArrayInputStream(buff);
ZipInputStream zin = new ZipInputStream(inS);
ZipEntry entry = zin.getNextEntry();
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[lengthR];
int offset = -1;
while((offset = zin.read(buffer)) != -1) {//将流写入缓存buffer,如果流过大,则将缓存buffer写入out,然后继续读流
out.write(buffer, 0, offset);
}
JSONObject jsonReceive = JSONObject.fromObject(out.toString());
for(int i = 0; i < 300; i++) {
System.out.println(jsonReceive.get("list" + i));
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public static int b2i(byte[] b) {
int value = 0;
for (int i = 0; i < 4; i++) {
int shift = (4 - 1 - i) * 8;
value += (b[i] & 0x000000FF) << shift;
}
return value;
}
}
分享到:
相关推荐
用VB.NET编写的压缩与解压函数,此函数可对保存成二进制数据的WORD文档内容进行很大的压缩,使系统读取大容量数据的速度大大地加快。
基于GUI用IO流中的ZipOutputStream ,ZipInputStream实现文件的解压与压缩, 如文件解压函数如下 private void unZip(String zipFileName, String outputDirectory)throws Exception { InputStream in=null; ...
字符串采用UTF-8编码获得byte数组,保证两端通用,如果应用对编码有要求,两端同时改为其他编码方式也可以 从Java和C#的代码看,两者代码上有细微差别,但是思路方面两者基本是一样的 另外一个备忘,Java里边,...
lz4:接收bytea类型的数据,返回压缩后的bytea类型数据 un_lz4:接收bytea类型的数据,返回解压后的bytea类型数据 lz4_utf8:接收utf8编码的文本数据,返回解压后的bytea类型数据 un_lz4_utf8:接收bytea类型的数据...
实现一种ARGB数据(二进制数据)的无损压缩/解压单元,用于GPU或其它存储器图形图像访问密集的系统中,利用无损数据压缩技术降低存储器带宽,提高访问效率。总体要求: 研究一种高效的ARGB数据压缩/解压算法; ...
//打开需压缩文件作为文件输入流 FileInputStream fin=new FileInputStream(args[0]); //建立压缩文件输出流 FileOutputStream fout=new FileOutputStream(args[1]); //建立gzip压缩输出流 GZIPOutputStream ...
实现一种ARGB数据(二进制数据)的无损压缩/解压单元,用于GPU或其它存储器图形图像访问密集的系统中,利用无损数据压缩技术降低存储器带宽,提高访问效率。总体要求: 研究一种高效的ARGB数据压缩/解压算法; ...
实现一种ARGB数据(二进制数据)的无损压缩/解压单元,用于GPU或其它存储器图形图像访问密集的系统中,利用无损数据压缩技术降低存储器带宽,提高访问效率。总体要求: 研究一种高效的ARGB数据压缩/解压算法; ...
List<Byte> list = new ArrayList(); for (int i = 0; i (); ) { int count = 1; boolean flag = true; Byte b = null; while (flag) { String key = stringBuilder.substring(i, i + count); b = map.get...
Java实现压缩与解压缩ZIP import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream;...
使用以下代码进行压缩和解压 /// /// 利用ICSharpCode压缩 /// /// <param name="param"></param> /// <returns></returns> public static string IC_Compress(string param) { byte[] data = System....
* @param zos 压缩输出流 * @param relativePath 相对路径 * @param absolutPath 文件或文件夹绝对路径 * @throws IOException * void */ private static void zip(ZipOutputStream zos, String ...
有时,您只需要压缩bytea对象,然后再将其返回给客户端即可。 有时,您会从客户端收到压缩的bytea ,因此必须先对其进行解压缩,然后才能使用它。 此扩展用于此。 此扩展名不适用于存储压缩。 如果您的元组足够大...
该软件包为提供流接口,并为LZ4数据块提供低级压缩和解压缩功能。 该实现基于参考C 。 安装 假设您已经安装了go工具链: go get github.com/pierrec/lz4 有一个命令行界面工具可以压缩和解压缩LZ4文件。 go ...
unity中压缩和解压byte[]时要引用的ICSharpCode.SharpZipLib.dll
12 * 普通用 Winrar 压缩的文件该解压不能通过 13 * Modify By HJ 2007-10-25 14 */ 15namespace ZipApplication 16{ 17 /// 18 /// 压缩类 19 /// 20 public class ZipClass 21 { 22 /// 23 /// ...
使用我自己包,直接放到WEB-INF\classes下解压即可, 在程序中加上 outf.setEncoding("gbk");即可 下面是我的多个文件压缩成一个的压缩,参考 String zipf="D:\\xxx\\xx\\xxb\\xx\\xz.zip"; //---------修改路径---...
该软件包提供了一个纯接口,用于压缩和解压缩表示为惰性ByteString的数据流。 它使用zlib C库,因此具有高性能。 它支持zlib , gzip和raw压缩格式。 它提供了一个方便的高级API,适用于大多数任务,并且在需要更多...
在python的时候就习惯使用zlib进行网页压缩。 golang下同样使用zlib进行压缩解压缩。 zlib官方给出的方法很简单,这里权当一个补充. zlib.NewWriter() 只能传递 []byte类型数据. NewWriterLevel 可以传递压缩的...
/* with byte order and alignment. Most of the decrunch functions */ /* encourage overruns in the buffers to make things as fast as possible. */ /* All the time is taken up in crc_calc() and decrunch()...