Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.*;
- //JZip Client
- public class NetZip {
- //Declaring private variables.
- private Socket socket = null;
- private File file = null;
- public static void main(String[] args) {
- try {
- File file = new File(args[0]);
- File newFile = new File(args[0]+".gz");
- }
- catch (ArrayIndexOutOfBoundsException error) {
- System.out.println("Please Enter a Filename!");
- }
- }
- public void toServer() throws IOException{
- //Creating socket
- socket = new Socket("localhost", 9999);
- //Creating stream to read from file.
- DataInputStream fileIn = new DataInputStream(
- new BufferedInputStream(
- new FileInputStream(
- file)));
- //Creating stream to write to socket.
- DataOutputStream dataOut = new DataOutputStream(
- new BufferedOutputStream(
- socket.getOutputStream()));
- byte[] buffer = new byte[1024];
- int len;
- //While there is data to be read, write to socket.
- while((len = fileIn.read(buffer)) != -1){
- System.out.println("Attempting to Write " + file
- + "to server.");
- dataOut.write(buffer, 0, len);
- }
- }
- //Read data from the serversocket, and write to new .gz file.
- public void fromServer() throws IOException{
- DataInputStream dataIn = new DataInputStream(
- new BufferedInputStream(
- socket.getInputStream()));
- byte[] buffer = new byte[1024];
- int len;
- while((len = dataIn.read(buffer)) != -1){
- // ???
- }
- }
- }
- import java.io.*;
- import java.net.*;
- import java.util.zip.GZIPOutputStream;
- //JZip Server
- public class ZipServer {
- private ServerSocket serverSock = null;
- private Socket socket = null;
- public ZipServer() throws IOException {
- //Creating server socket, and accepting from other sockets.
- try{
- serverSock = new ServerSocket(9999);
- Socket socket = serverSock.accept();
- }
- catch(IOException error){
- System.out.println("Error! Cannot create socket on port");
- }
- //Reading Data from socket
- DataInputStream dataIn = new DataInputStream(
- new BufferedInputStream(
- socket.getInputStream()));
- //Creating output stream.
- DataOutputStream zipOut= new DataOutputStream(new GZIPOutputStream(
- new BufferedOutputStream(
- socket.getOutputStream())));
- byte[] buffer = new byte[1024];
- int len;
- //While there is data to be read, write to socket.
- while((len = dataIn.read(buffer)) != -1){
- System.out.println("Attempting to Compress " + dataIn
- + "and send to client");
- zipOut.write(buffer, 0, len);
- }
- dataIn.close();
- zipOut.flush();
- zipOut.close();
- }
- public static void main(String[] args) throws IOException{
- ZipServer run = new ZipServer();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement