0

My task is to tar list of files and this tar should be kept in .gz file. I have succeeded in creating .tar file, but the next step like when creating .gz file I got an issue. After creating .gz file with a tar inside is converted into File format without any extension.

java code to generate .tar file

FileSystem fs = FileSystems.getDefault();
        OutputStream tar_output = new FileOutputStream(fs.getPath(hardLink.toString(), tarFileName + ".tar")
                .toFile());
        ArchiveOutputStream my_tar_ball = new ArchiveStreamFactory().createArchiveOutputStream(
                ArchiveStreamFactory.TAR, tar_output);
        Path p = null;
        TarArchiveEntry tar_file = null;
        for (String list : certFileList) {
            p = fs.getPath(hardLink.toString(), list);
            tar_file = new TarArchiveEntry(p.getFileName().toString());
            tar_file.setSize(p.toFile().length());
            my_tar_ball.putArchiveEntry(tar_file);
            IOUtils.copy(new FileInputStream(p.toFile()), my_tar_ball);
            my_tar_ball.closeArchiveEntry();
        }

call gzipIt function

gzipIt(hardLink.toString() + "/" + tarFileName + ".gz", hardLink.toString() + "/" + tarFileName + ".tar");

Java code to generate .gz file

public static void gzipIt(String outputDir, String sourceFile) {

    byte[] buffer = new byte[1024];

    try {

        FileOutputStream fos = new FileOutputStream(new File(outputDir));
        GZIPOutputStream gzos = new GZIPOutputStream(fos);

        FileInputStream in = new FileInputStream(sourceFile);

        int len;
        while ((len = in.read(buffer)) > 0) {
            gzos.write(buffer, 0, len);
        }

        in.close();

        gzos.finish();
        gzos.close();

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

Could anyone help me in resolving the issue.

Regards Sai

Yaswanth
  • 483
  • 1
  • 9
  • 25
  • add ".gz" to the output file name. – jtahlborn Nov 03 '14 at 05:01
  • Sorry I missed a line, just now I added it. while calling the function I am sending .gz file extension as shown in code – Yaswanth Nov 03 '14 at 05:29
  • and what exactly is your question? – jtahlborn Nov 03 '14 at 13:37
  • I have to create .tar file first with list of files and this .tar file should be placed in .gz file. **It is clear, we have to place list of files in .tar file and this .tar file should be placed in .gz file.** Now when I am checking .gz file by extracting, it contained file which have no file type extension actually it should be .tar file inside the .gz file. please check the url for image[http://s27.postimg.org/uc3snkpmr/file_Format.jpg] – Yaswanth Nov 04 '14 at 04:49
  • gzip files don't have "internal names". you have the tar file, it just doesn't have the extension. you could change your code to name the gzip file `tarFileName + ".tar.gz"`. – jtahlborn Nov 04 '14 at 13:37
  • Actually gzip files can carry metadata - including a file name. Apache Commons Compress' `GzipCompressorOutputStream` allows you to specify them since release 1.7. – Stefan Bodewig Feb 08 '15 at 20:36

0 Answers0