title: How to compress a file using java on hdfs
body: <p>I'm new to hdfs/hadoop and need to know how to compress a file that is in a hdfs dir like
  hdfs://sandbox:8020/some/path.</p>
  
  <p>I have tried </p>
  
  <pre><code>      Path p = new Path("/my/path/test1.gz");
        FSDataOutputStream os = fs.create(p);
  
        GZIPOutputStream gzipOs = new GZIPOutputStream(new BufferedOutputStream(os));
  
        Path filePath = file.getPath();
        FSDataInputStream is = fs.open(filePath);
  
        System.out.println("Writing gzip");
  
        byte[] buffer = new byte[1024];
        int len;
        while((len= is.read(buffer)) != -1){
          gzipOs.write(buffer, 0, len);
        }
        //close resources
        is.close();
        gzipOs.close();
  </code></pre>
  
  <p>But it doesn't work. </p>
  
  <p>Any suggestions? Thanks in advance.</p>