圧縮解凍byte[]ストリーム

3712 ワード

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