Results 1 to 5 of 5
  1. #1
    Milanek is offline Member
    Join Date
    Oct 2011
    Posts
    6
    Rep Power
    0

    Default Compress and decompress streams

    Hello guys,

    I found this article about simple proxy server implemented in JAVA:

    A simple proxy server : Proxy Server**Network Protocol**Java

    The code simply gets some stream from the client, after sends it to the server and after it gets stream from the server and sends the respone to the client. What I would like to do is to compress this streams before it is sent and decompress after it is received.

    I found the class GZIPInputStream but Im not sure how to use it and what I found on internet didnt help me. I either didnt understand that so much or it was not a good solution for me.

    My idea is too that but Im not sure if its ok:


    Java Code:
     
    final InputStream streamFromClient = client.getInputStream();
    final OutputStream streamToClient = client.getOutputStream();
    final InputStream streamFromServer = server.getInputStream();
    final OutputStream streamToServer = server.getOutputStream();
    
    InputStream gzipStream = new GZIPInputStream(streamFromClient );
    try 
    {
            while ((bytesRead = gzipStream.read(request)) != -1)
           {
                        streamToServer.write(request, 0, bytesRead);
                        streamToServer.flush();
            }
    }
    catch (Exception e) {
    System.out.println(e);
    }
    Now the data sent to the server should be compressed before sending (but im not sure if its a correct solution). IS IT?


    Now imagine the server sends me the compressed data.
    So this stream:
    Java Code:
     final InputStream streamFromServer = server.getInputStream();
    is compressed.

    How can I decompress it and write to the

    Java Code:
      final OutputStream streamToClient = client.getOutputStream();
    Thank you for your help!
    Milan

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: Compress and decompress streams

    Have you written a simple test program that writes & compresses and then reads & decompresses to work out the details on how to use those classes? You could use a byte array for writing to and reading from.

  3. #3
    Milanek is offline Member
    Join Date
    Oct 2011
    Posts
    6
    Rep Power
    0

    Default Re: Compress and decompress streams

    Thanks, I have but I still didnt figure it out. Any help?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default Re: Compress and decompress streams

    Post your code for the simple test program with your questions or the error messages and we can get it to work.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Compress and decompress streams

    Cross-post: stackoverflow.com: compressing-and-decompressing-streams

    To the original poster, cross-posting can frustrate anyone who tries to help you only to find out later that the same answer was given hours ago in a cross-posted thread. No one likes wasting their time, especially a volunteer. The polite thing to do would be to provide links in both cross-posts to each other.

Similar Threads

  1. Java Applet decompress files zip, rar etc
    By Phoenix Dogstar in forum Java Applets
    Replies: 5
    Last Post: 04-16-2011, 09:38 PM
  2. Decompress (un-gzip) a byte[]?
    By NeuroFuzzy in forum New To Java
    Replies: 2
    Last Post: 02-10-2011, 02:12 PM
  3. Compress text in java
    By rakesh_n_mehta in forum Threads and Synchronization
    Replies: 2
    Last Post: 10-21-2009, 09:12 PM
  4. Help with a Compress File
    By Thomasheen in forum New To Java
    Replies: 1
    Last Post: 08-31-2009, 03:34 PM
  5. how to compress of arraylist
    By hai789 in forum Web Frameworks
    Replies: 0
    Last Post: 05-20-2008, 12:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •