0

I have the contents I want to write to a file saved in Strings in my Java program. I need o write them to a .txt file and then compress them into a .gz archive. I'm able to do all of this, but when I extract the file from the .gz archive, the extracted file no longer bear's the .txt file extension.

This is my code below:

private void fillFileBody(OutputStream outputStream) throws IOException {
    Scanner scanner = new Scanner(new ByteArrayInputStream(this.fileBody.getBytes()));

    while(scanner.hasNextLine()){
        outputStream.write((scanner.nextLine() + "\n").getBytes());
    }

    scanner.close();
}

public void writeFileContentsToDisk(){
    GZIPOutputStream gZipOutStream = null;
    FileOutputStream initialTxtFileOutStream = null;
    FileInputStream initialTxtFileInStream = null;

    String txtFile = this.fileName + ".txt";

    try {
        initialTxtFileOutStream = new FileOutputStream(txtFile);
        initialTxtFileOutStream.write((this.fileHeader).getBytes());
        fillFileBody(initialTxtFileOutStream);
        initialTxtFileOutStream.write(this.fileTrailer.getBytes());

        initialTxtFileInStream = new FileInputStream(txtFile);
        gZipOutStream = new GZIPOutputStream(new FileOutputStream(this.fileName + ".gz"));

        byte[] buffer = new byte[1024];
        int length;
        while ((length = initialTxtFileInStream.read(buffer)) > 0) {
            gZipOutStream.write(buffer, 0, length);
        }
    } catch (Exception e) {
        LOGGER.error("Error writing file: " + Logger.getStackTrace(e));
    }

    try {
        gZipOutStream.close();
        initialTxtFileOutStream.close();
        initialTxtFileInStream.close();
    } catch (IOException e) {
        LOGGER.warn("Error closing i/o streams involved in writing file: " + Logger.getStackTrace(e));
    }   
}
shinvu
  • 601
  • 2
  • 7
  • 23

1 Answers1

0

I wrote the sample code below and used x.c as input from my working directory. x.c.gz was the output file. I used gunzip on x.c.gz and it produced a file with the name x.c and so it seemed to work fine.

package compress;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;


import java.util.zip.GZIPOutputStream;
/**
 *
 * @author srikanthn
 */
public class Compress {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        compressFile(args[0]);
    }

    public static void compressFile(String file){
        GZIPOutputStream gZipOutStream = null;
        FileInputStream initialTxtFileInStream = null;

        String txtFile =  file;

        try {
            initialTxtFileInStream = new FileInputStream(txtFile);
            gZipOutStream = new GZIPOutputStream(new FileOutputStream(txtFile+ ".gz"));

            byte[] buffer = new byte[1024];
            int length;
            while ((length = initialTxtFileInStream.read(buffer)) > 0) {
                gZipOutStream.write(buffer, 0, length);
            }
        } catch (IOException e) {
            System.out.println("Error writing file: " + e.getStackTrace());
        }

        try {
            gZipOutStream.close();
            initialTxtFileInStream.close();
        } catch (IOException e) {
            System.out.println("Error closing i/o streams involved in writing file: " + e.getStackTrace());
        }   
    }
}
Srikanth N
  • 137
  • 1
  • 4
  • No....that didn't work. It just makes the output file `fileName.txt.gz`. When I extract it, I'm still only getting a file named `fileName`. – shinvu Mar 01 '18 at 06:34
  • I realized I made an error with my comment above because all I did was to make file name have a .txt in it. How are you extracting the file? Are you using some command to it? Or through code? – Srikanth N Mar 01 '18 at 07:32
  • The extraction is done manually through an application like PKZIP or 7ZIP. But the packaging needs to be only through code – shinvu Mar 01 '18 at 11:47
  • Is it even possible in Java? – shinvu Mar 01 '18 at 11:47
  • See my other answer that I about to post next. – Srikanth N Mar 03 '18 at 01:10
  • I actually edited the original answer (see above). It worked for me with gunzip. With Regarding your other comment about what can we do in java, it seems like we can readin a gz file with GZipInputStream but when we write that uncompressed stream out, we will have to pick a file name. – Srikanth N Mar 03 '18 at 01:17