压缩解压byte[]流

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;
  }
}

 

你可能感兴趣的:(byte[])