title: compress file with GZIP in java
body: <p>I tried to read a txt file with the buffered input stream, and compress it with GZIP, it worked. However, when I try to extract the compressed file with the zip, the file seems in unreadable binary format, how do I solve this problem?  The following is my code:</p>
  
  <pre><code>public static void main(String[] args) {
      compressWithGZIP(SAVE_PATH2, SAVE_PATH3);
      //uncompressWithGZIP(SAVE_PATH3 + "compressed.gz", SAVE_PATH4);
  }
  
  private static void uncompressWithGZIP(String oripath, String outputPath) {
      BufferedInputStream bi = null;
      BufferedOutputStream bo = null;
      try {
          bi = new BufferedInputStream(new GZIPInputStream(
                  new FileInputStream(oripath)));
          bo = new BufferedOutputStream(new FileOutputStream(outputPath));
          int c;
          while ((c = bi.read()) != -1) {
              bo.write(c);
          }
      } catch (FileNotFoundException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } finally {
          try {
              if (bi != null) {
                  bi.close();
              }
              if (bo != null) {
                  bo.close();
              }
          } catch (Exception e) {
              e.printStackTrace();
  
          }
      }
  }
  
  private static void compressWithGZIP(String filePath, String outputPath) {
      if (outputPath == null || outputPath.isEmpty()
              || !outputPath.endsWith(".gz")) {
          outputPath += "compressed.gz";
      }
  
      BufferedReader br = null;
      BufferedOutputStream bo = null;
      try {
          br = new BufferedReader(new FileReader(filePath));
          bo = new BufferedOutputStream(new GZIPOutputStream(
                  new FileOutputStream(outputPath)));
          int c;
          while ((c = br.read()) != -1) {
              bo.write(c);
          }
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      } finally {
          try {
              if (br != null) {
                  br.close();
              }
              if (bo != null) {
                  bo.close();
              }
          } catch (Exception e) {
              e.printStackTrace();
  
          }
      }
  
  }
  </code></pre>