Learn how to merge multiple ZIP and TAR archives in C# .NET: https://blog.aspose.com/2022/01/06/merge-zip-and-tar-files-in-csharp/
Last active
January 6, 2022 13:59
-
-
Save aspose-com-gists/1ca1a64b63a37ce4b2f6b47b86b922e5 to your computer and use it in GitHub Desktop.
Merge Multiple ZIP or TAR Archives in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Load TAR archive | |
using (TarArchive tarArchive = new TarArchive("Archives/SourceArchive.tar")) | |
{ | |
// Load ZIP archive | |
using (Archive zipArchive = new Archive("Archives/DestinationArchive.zip")) | |
{ | |
// Iterate over the entries | |
foreach (TarEntry entry in tarArchive.Entries) | |
{ | |
// Check is entry is not a directory. Entries that are directories are skipped, but their files are added respecting relative paths. | |
if (!entry.IsDirectory) | |
{ | |
// Load entry to memory stream | |
MemoryStream mem = new MemoryStream(); | |
entry.Open().CopyTo(mem); | |
// Add entry to ZIP | |
zipArchive.CreateEntry(entry.Name, mem); | |
} | |
} | |
// Save merged archive separately | |
zipArchive.Save("Archives/merged-archive.zip"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Load the source TAR archive | |
using (TarArchive sourceArchive = new TarArchive("Archives/SourceArchive.tar")) | |
{ | |
// Load destination TAR archive | |
using (TarArchive destArchive = new TarArchive("Archives/DestinationArchive.tar")) | |
{ | |
// Iterate over the entries | |
foreach (TarEntry entry in sourceArchive.Entries) | |
{ | |
// Check is entry is not a directory. Entries that are directories are skipped, but their files are added respecting relative paths. | |
if (!entry.IsDirectory) | |
{ | |
// Load entry to memory stream | |
MemoryStream mem = new MemoryStream(); | |
entry.Open().CopyTo(mem); | |
// Add entry to destination TAR | |
destArchive.CreateEntry(entry.Name, mem); | |
} | |
} | |
// Save merged archive separately | |
destArchive.Save("Archives/merged-archive.tar"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Load the source ZIP archive | |
using (Archive zipArchive = new Archive("Archives/SourceArchive.zip")) | |
{ | |
// Load destination TAR archive | |
using (TarArchive tarArchive = new TarArchive("Archives/DestinationArchive.tar")) | |
{ | |
// Iterate over the entries | |
foreach (ArchiveEntry entry in zipArchive.Entries) | |
{ | |
// Check is entry is not a directory. Entries that are directories are skipped, but their files are added respecting relative paths. | |
if (!entry.IsDirectory) | |
{ | |
// Load entry to memory stream | |
MemoryStream mem = new MemoryStream(); | |
entry.Open().CopyTo(mem); | |
// Add entry to destination TAR | |
tarArchive.CreateEntry(entry.Name, mem); | |
} | |
} | |
// Save merged archive separately | |
tarArchive.Save("Archives/merged-archive.tar"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Load the source ZIP archive | |
using (Archive sourceArchive = new Archive("Archives/SourceArchive.zip")) | |
{ | |
// Load destination ZIP archive | |
using (Archive destArchive = new Archive("Archives/DestinationArchive.zip")) | |
{ | |
// Iterate over the entries | |
foreach (ArchiveEntry entry in sourceArchive.Entries) | |
{ | |
// Check is entry is not a directory. Entries that are directories are skipped, but their files are added respecting relative paths. | |
if (!entry.IsDirectory) | |
{ | |
// Load entry to memory stream | |
MemoryStream mem = new MemoryStream(); | |
entry.Open().CopyTo(mem); | |
// Add entry to destination ZIP | |
destArchive.CreateEntry(entry.Name, mem); | |
} | |
} | |
// Save merged archive separately | |
destArchive.Save("Archives/merged-archive.zip"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment