-1

how to write a csv file to a gzip in java. While writingmy csv type is getting changed to file type. I want it to be .csv??? this is the code i used...

public class zipWrite extends commonParts{

    public static void zip() throws Exception{

        zipWriter=  new FileOutputStream("C:\\Users\\myzip.gz");
        outputStream = new GZIPOutputStream(zipWriter);
        zipIn = new FileInputStream("C:\\Users\\mycsv.csv");
        byte []buffer = new byte[1024];
        int length;
        while ((length = zipIn.read(buffer)) > 0) {
            outputStream.write(buffer, 0, length);
        }
        zipIn.close();              
        outputStream.close();
        } 

}
Malt
  • 28,965
  • 9
  • 65
  • 105
Francis Sunny
  • 36
  • 1
  • 6

1 Answers1

0

If you mean that the CSV file contained int the output Gzip file does not keep the CSV extension, the solution is to define the output file name as "myzip.csv.zip":

  private static final String INPUT_FILE = "C:\\Users\\mycsv.csv";
  private static final String OUTPUT_FILE = "C:\\Users\\myzip.csv.gz";

  public static void zip() throws IOException {
    byte[] buffer = new byte[2048];
    FileInputStream inputStream = new FileInputStream(INPUT_FILE);
    FileOutputStream outputStream = new FileOutputStream(OUTPUT_FILE);
    GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outputStream);
    int length;
    while ((length = inputStream.read(buffer)) > 0) {
      gzipOutputStream.write(buffer, 0, length);
    }
    inputStream.close();
    gzipOutputStream.close();
  }

The file inside the gzip will contain the "myzip.csv" file.

lsantamaria
  • 178
  • 1
  • 2
  • 12