Skip to main content

Compress File in GZip Format using java


package com.pukhraj.blog;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.GZIPOutputStream;

public class CompressFile {

  public static void main(String[] args) throws Exception {

    InputStream in = new FileInputStream("InputFilePath/fileName.txt");
    OutputStream out = new FileOutputStream("OutputFilePath/fileName.txt.gz");
    GZIPOutputStream gzout = new GZIPOutputStream(out);
    for (int c = in.read(); c != -1; c = in.read()) {
      gzout.write(c);
    }
    gzout.close();
  }
}

Comments

Popular posts from this blog

Common Eclipse Shortcuts for Java Developer Open Resource : ctrl+shift+r Quick Outline : ctrl+o Assign to local variable : ctrl+2, L Rename : alt+shift+r  Extract Local Variable : alt+shift+l  Extract Method : alt+shift+m
How to check a string starts with numeric number? String str = "123abcd"; boolean isStartWithDigit = Character.isDigit(s.charAt(0)); System.out.println("isStartWithDigit: "+isStartWithDigit); How to get first numeric value from a string in java?  Matcher matcher = Pattern.compile("\\d+").matcher(titleStr); if(matcher.find()) { System.out.println(matcher.group()); }; How to check if a String contains another String in a case insensitive manner in Java? Solution 1: String str = "I am Java Developer"; String test = "java"; Boolean bool = str.toLowerCase().contains(test.toLowerCase()); System.out.println(bool); Solution 2: org.apache.commons.lang3.StringUtils.containsIgnoreCase("AbBaCca", "bac"); Solution 3: Pattern.compile(Pattern.quote(s2), Pattern.CASE_INSENSITIVE).matcher(s1).find();