in the below code i can able to convert a file to gzip but here i am providing the location for input & output staticly. But i need to provide the file name dynamically
example here i am using
String source_filepath = "C:\\Users\\abc\\Desktop\\home6.jpg";
String destinaton_zip_filepath =C:\\Users\\abc\\Desktop\\home6.gzip";
here in place of home6.jpg i can give anything dynamically
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;
public class CompressFileGzip {
public static void main(String[] args) {
String source_filepath = "C:\\Users\\abc\\Desktop\\home6.jpg";
String destinaton_zip_filepath = "C:\\Users\\abc\\Desktop\\home6.gzip";
CompressFileGzip gZipFile = new CompressFileGzip();
gZipFile.gzipFile(source_filepath, destinaton_zip_filepath);
}
public void gzipFile(String source_filepath, String destinaton_zip_filepath) {
byte[] buffer = new byte[1024];
try {
FileOutputStream fileOutputStream =new FileOutputStream(destinaton_zip_filepath);
GZIPOutputStream gzipOuputStream = new GZIPOutputStream(fileOutputStream);
FileInputStream fileInput = new FileInputStream(source_filepath);
int bytes_read;
while ((bytes_read = fileInput.read(buffer)) > 0) {
gzipOuputStream.write(buffer, 0, bytes_read);
}
fileInput.close();
gzipOuputStream.finish();
gzipOuputStream.close();
System.out.println("The file was compressed successfully!");
} catch (IOException ex) {
ex.printStackTrace();
}
}
}