Archiving a directory does not preserve the directory name (Tar) #273

Closed
opened 2026-01-29 22:09:23 +00:00 by claunia · 3 comments
Owner

Originally created by @SimonCahill on GitHub (Jan 31, 2018).

After attempting to restore a backup file, I could not determine where each archive should be extracted to.
Other archives I'd tested, created using the .tar command, worked just fine and work as expected.

The command

    $ tar -cvf ../../backup/backup_test.tar ./oegdarchiv

Creates an archive as follows:
working_archive

Creating an archive, on the other hand, using SharpCompress, creates an archive looking like this:
broken_archive
Which is essentially only the contents of the parent directory.
If I were only backing up individual archives, this wouldn't be an issue. However, multiple archives are backed up, and so the re.store files (containing the location to restore an individual archive to) are worthless, seeming as they are somehow all placed in the root directory of the tar.

    // Create new tarball
    var writerOptions = new WriterOptions(PluginConfig.GetBool("compress_tarballs") ? SharpCompress.Common.CompressionType.GZip : SharpCompress.Common.CompressionType.None);
        using (var fStream = tmp.Create())
        using (var tar = WriterFactory.Open(fStream, SharpCompress.Common.ArchiveType.Tar, writerOptions)) {
            tmp.Attributes = FileAttributes.Archive | FileAttributes.Compressed | FileAttributes.Temporary; // Set file attributes
                    
            Info("Enumerating archives...");
            foreach (var archive in backupDirectories) {
                Info("Processing {0}...", archive.FullName);
                var restoreLocationFile = Path.Combine(archive.FullName, RestoreLocationFilename);
                File.WriteAllText(restoreLocationFile, string.Concat(archive.FullName, '\n'));
                tar.WriteAll(archive.FullName, option: SearchOption.AllDirectories);
                File.Delete(restoreLocationFile);
            }
        }

I'm going to go out on a limb here, and say I'm essentially executing very similar commands, which should both result in the same output.
I can't simply point to the parent directory, as not all directories in said parent directory are viable for backing up.

Any help on the matter is much appreciated.

Originally created by @SimonCahill on GitHub (Jan 31, 2018). After attempting to restore a backup file, I could not determine where each archive should be extracted to. Other archives I'd tested, created using the .tar command, worked just fine and work as expected. The command ```bash $ tar -cvf ../../backup/backup_test.tar ./oegdarchiv ``` Creates an archive as follows: ![working_archive](https://user-images.githubusercontent.com/3124521/35629121-20e7c756-069e-11e8-9067-727a16d06bf6.PNG) Creating an archive, on the other hand, using SharpCompress, creates an archive looking like this: ![broken_archive](https://user-images.githubusercontent.com/3124521/35629119-20cc0d9a-069e-11e8-9bd8-e69882660907.PNG) Which is essentially only the contents of the parent directory. If I were only backing up individual archives, this wouldn't be an issue. However, multiple archives are backed up, and so the re.store files (containing the location to restore an individual archive to) are worthless, seeming as they are somehow all placed in the root directory of the tar. ```csharp // Create new tarball var writerOptions = new WriterOptions(PluginConfig.GetBool("compress_tarballs") ? SharpCompress.Common.CompressionType.GZip : SharpCompress.Common.CompressionType.None); using (var fStream = tmp.Create()) using (var tar = WriterFactory.Open(fStream, SharpCompress.Common.ArchiveType.Tar, writerOptions)) { tmp.Attributes = FileAttributes.Archive | FileAttributes.Compressed | FileAttributes.Temporary; // Set file attributes Info("Enumerating archives..."); foreach (var archive in backupDirectories) { Info("Processing {0}...", archive.FullName); var restoreLocationFile = Path.Combine(archive.FullName, RestoreLocationFilename); File.WriteAllText(restoreLocationFile, string.Concat(archive.FullName, '\n')); tar.WriteAll(archive.FullName, option: SearchOption.AllDirectories); File.Delete(restoreLocationFile); } } ``` I'm going to go out on a limb here, and say I'm essentially executing very similar commands, which should both result in the same output. I can't simply point to the parent directory, as not all directories in said parent directory are viable for backing up. Any help on the matter is much appreciated.
Author
Owner

@adamhathcock commented on GitHub (Jan 31, 2018):

Just prepend the directory name when writing the name of the entry.

This is code, not a full program thing like the tar executable. Less is done for you. It's not "worthless"

@adamhathcock commented on GitHub (Jan 31, 2018): Just prepend the directory name when writing the name of the entry. This is code, not a full program thing like the tar executable. Less is done for you. It's not "worthless"
Author
Owner

@adamhathcock commented on GitHub (Jan 31, 2018):

The WriteAll method just doesn't do what you want (prepend directory). You can write the loop yourself or submit a pull request with a new option.

@adamhathcock commented on GitHub (Jan 31, 2018): The WriteAll method just doesn't do what you want (prepend directory). You can write the loop yourself or submit a pull request with a new option.
Author
Owner

@SimonCahill commented on GitHub (Jan 31, 2018):

I never said it was worthless, merely that it doesn't work as expected ;)

I'll check out the source and see what I can do!
Thanks!

@SimonCahill commented on GitHub (Jan 31, 2018): I never said it was worthless, merely that it doesn't work as expected ;) I'll check out the source and see what I can do! Thanks!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#273