1

I have a set of files in which there are some hard links pointing to some of the files. For Example:

/tmp/test/file1
/tmp/test/file1_Link

The file1 is a file with size 1 MB. file1_Link is a hard link pointing to file1

When I use the unix tar command to tar these both files , the resulting archive has a size of 1 MB (mytar.tar)

tar -cvf ../mytar.tar .
-rw-r--r--  1 bsarraf  189060905   1.0M Feb 11 22:06 mytar.tar
-rw-r--r--  1 bsarraf  189060905   2.0M Feb 11 22:10 files.tar

However when I use the apache commons compress and add all the files in the tar file, I get a tar with size 2 MB (files.tar).

This means in the tar the same file was added twice.

How can I add the hard link file in the tar as a hard link of the other file?

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • You didn't show your code. I believe you have to detect hard links and handle the link entry creation (within the tar file) yourself. `org.apache.commons.compress.archivers.tar.TarConstants` contains type constants for both soft (`LF_SYMLINK`) and hard (`LF_LINK`) links. – Jim Garrison Feb 12 '17 at 06:49
  • Thanks @JimGarrison for your response. I was able to resolve the issue with some experiment. – Bijay Kumar Sarraf Feb 12 '17 at 09:09

1 Answers1

3

For others who might have the same issue, I was able to solve the issue with the following approach: 1. Identify if the file is a hard link, there are various ways to do this. 2. Once hardlinks are identified, use the following method to add the hardline file in the tar

private void addLinkFileToTar(TarArchiveOutputStream tar, File linkFile, File file, String base)
        throws IOException {
    String entryName = base + linkFile.getName();
    TarArchiveEntry tarEntry = new TarArchiveEntry(entryName, TarConstants.LF_LINK);
    tarEntry.setLinkName(base+file.getName());
    tar.putArchiveEntry(tarEntry);
    tar.closeArchiveEntry();
}

here linkFile is the hard Link file and file is the target File.