简体   繁体   English

我如何编辑此代码,以便服务器在发送到客户端之前先对文件进行压缩,而在接收后客户端将其解压缩

[英]How I can edit this code, so that server zips the file before sending to the client and Client unzips this after receiving

I want that the server should zip the file before sending to the client and the client unzips the file after getting from server. 我希望服务器在发送到客户端之前应该先压缩文件,然后客户端从服务器获取文件后再将文件解压缩。 I don't know how I can achieve this. 我不知道该如何实现。 Anyone can help me in this please? 有人可以帮我吗?

Server side code 服务器端代码

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
public class SimpleFileServer {

  public final static int SOCKET_PORT = 13267; 
  public final static String FILE_TO_SEND = "c:/temp/xyz.txt";  

  public static void main (String [] args ) throws IOException {
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    OutputStream os = null;
    ServerSocket servsock = null;
    Socket sock = null;
    try {
      servsock = new ServerSocket(SOCKET_PORT);
      while (true) {
        System.out.println("Waiting...");
        try {
          sock = servsock.accept();
          System.out.println("Accepted connection : " + sock);
          // send file
          File myFile = new File (FILE_TO_SEND);
          byte [] mybytearray  = new byte [(int)myFile.length()];
          fis = new FileInputStream(myFile);
          bis = new BufferedInputStream(fis);
          bis.read(mybytearray,0,mybytearray.length);
          os = sock.getOutputStream();
          System.out.println("Sending " + FILE_TO_SEND + "(" + mybytearray.length + " bytes)");
          os.write(mybytearray,0,mybytearray.length);
          os.flush();
          System.out.println("Done.");
        }
        finally {
          if (bis != null) bis.close();
          if (os != null) os.close();
          if (sock!=null) sock.close();
        }
      }
    }
    finally {
      if (servsock != null) servsock.close();
    }
  }
}

Client side code: 客户端代码:

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

public class SimpleFileClient {

          public final static int SOCKET_PORT = 13267;     
          public final static String SERVER = "127.0.0.1";  
          public final static String FILE_TO_RECEIVED = "c:/temp/xyz.txt";  

public final static int FILE_SIZE = 6022386;  

  public static void main (String [] args ) throws IOException {
    int bytesRead;
    int current = 0;
    FileOutputStream fos = null;
    BufferedOutputStream bos = null;
    Socket sock = null;
    try {
      sock = new Socket(SERVER, SOCKET_PORT);
      System.out.println("Connecting...");

      // receive file
      byte [] mybytearray  = new byte [FILE_SIZE];
      InputStream is = sock.getInputStream();
      fos = new FileOutputStream(FILE_TO_RECEIVED);
      bos = new BufferedOutputStream(fos);
      bytesRead = is.read(mybytearray,0,mybytearray.length);
      current = bytesRead;

      do {
         bytesRead =
            is.read(mybytearray, current, (mybytearray.length-current));
         if(bytesRead >= 0) current += bytesRead;
      } while(bytesRead > -1);

      bos.write(mybytearray, 0 , current);
      bos.flush();
      System.out.println("File " + FILE_TO_RECEIVED
          + " downloaded (" + current + " bytes read)");
    }
    finally {
      if (fos != null) fos.close();
      if (bos != null) bos.close();
      if (sock != null) sock.close();
    }
  }

}

您可以使用GZIPInputStreamGZIPOutputStream分别装饰输入。

Answering for my own question 回答我自己的问题

package server;  
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
  import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;


public class SimpleFileServer {

  public final static int SOCKET_PORT = 1328;
  public final static String FILE_NAME = "from.txt";  
  public final static String FILE_TO_SEND = "c:/temp/from.txt";
  public final static String ZIP_FILE_TO_SEND = "c:/temp/myFile.zip";

  public static void main (String [] args ) throws IOException {
    FileInputStream fis = null;
    BufferedInputStream bis = null;
    OutputStream os = null;
    ServerSocket servsock = null;
    Socket sock = null;


    zipIt();

    try {
      servsock = new ServerSocket(SOCKET_PORT);
      while (true) {
        System.out.println("Waiting...");
        try {
          sock = servsock.accept();
          System.out.println("Accepted connection : " + sock);
          // send file
          File myFile = new File (ZIP_FILE_TO_SEND);
          byte [] mybytearray  = new byte [(int)myFile.length()];
          fis = new FileInputStream(myFile);
          bis = new BufferedInputStream(fis);
          bis.read(mybytearray,0,mybytearray.length);
          os = sock.getOutputStream();
          System.out.println("Sending " + ZIP_FILE_TO_SEND + "(" + mybytearray.length + " bytes)");
          os.write(mybytearray,0,mybytearray.length);
          os.flush();
          System.out.println("Done.");
        }
        finally {
          if (bis != null) bis.close();
          if (os != null) os.close();
          if (sock!=null) sock.close();          
        }
      }
    }
    finally {
      if (servsock != null) servsock.close();
    }
  }

  public static void zipIt()
    {
        byte[] buffer = new byte[1024];

        try{

            FileOutputStream fos = new FileOutputStream(ZIP_FILE_TO_SEND);
            ZipOutputStream zos = new ZipOutputStream(fos);
            ZipEntry ze= new ZipEntry(FILE_NAME);
            zos.putNextEntry(ze);
            FileInputStream in = new FileInputStream(FILE_TO_SEND);

            int len;
            while ((len = in.read(buffer)) > 0) {
                zos.write(buffer, 0, len);
            }

            in.close();
            zos.closeEntry(); 
            zos.close();

            System.out.println("Zip Done");

        }catch(IOException ex){
           ex.printStackTrace();
        }
    }
}

Client Side: 客户端:

package server;  
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class SimpleFileClient {

  public final static int SOCKET_PORT = 1328;      
  public final static String SERVER = "127.0.0.1";  
  public final static String
       FILE_NAME = "xyz.txt";  
  public final static String
       ZIP_FILE_NAME = "xyz.zip";  
  public final static String
       FILE_PATH = "c:/temp/";  

  public final static int FILE_SIZE = 6022386; 

  public static void main (String [] args ) throws IOException {
    int bytesRead;
    int current = 0;
    FileOutputStream fos = null;
    BufferedOutputStream bos = null;
    Socket sock = null;
    try {
      sock = new Socket(SERVER, SOCKET_PORT);
      System.out.println("Connecting...");

      // receive file
      byte [] mybytearray  = new byte [FILE_SIZE];
      InputStream is = sock.getInputStream();
      fos = new FileOutputStream(FILE_PATH+ZIP_FILE_NAME);
      bos = new BufferedOutputStream(fos);
      bytesRead = is.read(mybytearray,0,mybytearray.length);
      current = bytesRead;

      do {
         bytesRead =
            is.read(mybytearray, current, (mybytearray.length-current));
         if(bytesRead >= 0) current += bytesRead;
      } while(bytesRead > -1);

      bos.write(mybytearray, 0 , current);
      bos.flush();
      System.out.println("File " + FILE_PATH+ZIP_FILE_NAME
          + " downloaded (" + current + " bytes read)");
    }
    finally {
      if (fos != null) fos.close();
      if (bos != null) bos.close();
      if (sock != null) sock.close();
    }

    unZipIt();

  }



  public static void unZipIt(){

     byte[] buffer = new byte[1024];

     try{

        //get the zip file content
        ZipInputStream zis = 
        new ZipInputStream(new FileInputStream(FILE_PATH+ZIP_FILE_NAME));
        //get the zipped file list entry
        ZipEntry ze = zis.getNextEntry();

        while(ze!=null){

           String fileName = ze.getName();
           File newFile = new File(FILE_PATH+FILE_NAME);

           System.out.println("file unzip : "+ newFile.getAbsoluteFile()); 
           FileOutputStream fos = new FileOutputStream(newFile);             

            int len;
            while ((len = zis.read(buffer)) > 0) {
            fos.write(buffer, 0, len);
            }

            fos.close();   
            ze = zis.getNextEntry();
        }

        zis.closeEntry();
        zis.close();
        System.out.println("Done");

    }catch(IOException ex){
       ex.printStackTrace(); 
    }
   }    



}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何编写连接本地文件的客户端套接字流,用于发送和接收数据包 - How can I write the client socket stream which connects to local file for sending and receiving packets 如何使用RSocket创建发送客户端/服务器的文件? - How do I create a file sending client/server with RSocket? UDP客户端向服务器发送数据,但未收到响应 - UDP client sending data to server, but not receiving response 客户端/服务器的发送/接收字符串有问题 - Trouble sending/receiving strings for client/server 服务器向客户端发送空文件 - Server sending Empty file to client 客户端在发送到服务器之前应该如何编码Websocket消息? - How should the client encode a websocket message before sending to server? 如何修复客户端代码,以便通过组合获得代码重用? - How can I fix the client code so that I get code reuse via composition? 如何编写服务器/客户端视频和音频流应用程序? - How can I code a server/client video and audio streaming application? 收到FIN数据包后如何强制CXF客户端断开连接 - How can I force a CXF client to disconnect after receiving a FIN packet 如何使用服务器端Java加密sqlite db文件,以便flex / air客户端可以读取它? - How to encrypt a sqlite db file with server side java so a flex/air client can read it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM