Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active January 6, 2022 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/1ca1a64b63a37ce4b2f6b47b86b922e5 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/1ca1a64b63a37ce4b2f6b47b86b922e5 to your computer and use it in GitHub Desktop.
Merge Multiple ZIP or TAR Archives in C#
// 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");
}
}
// 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");
}
}
// 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");
}
}
// 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