Zip Conversion via C#

Convert ZIP to Various File Archive Formats

How to Convert Zip Using C#

Converting ZIP files using C# can be facilitated with Aspose.Zip for .NET, a powerful API that simplifies the process. The library enables developers to perform various operations, including extraction and creation of ZIP files, along with password protection and compression level customization. Using the relevant methods provided by Aspose.Zip, developers can easily handle ZIP file conversions in many popular archive file-format such RAR , CPIO , CAB , TAR and others.

Conversion one archive to another is essentially a combination of unpack of the first one and composition of the second. You can store interim extracted data in the RAM without flushing them to disk. Beware of memory consumption and know how much memory can you allocate to reside these entries.

Developer Guide - Archive Conversion

The provided code snippet demonstrates how to convert a RAR archive to a ZIP format in C#. It utilizes the Aspose.Zip library to easily extract the RAR entries and then create corresponding entries in the ZIP archive.

Let’s review this sample step-by-step:

  1. Instantiate ZIP Archive prepared for compressing. We’ll fill it with entries on next steps.
  2. Instantiate Rar Archive prepared for decompression, supplying path to the archive.
  3. Extract each entry to temporary storage.
  4. Compose a zip entry from that storage. If the entry represents a directory, we just use its path.
  5. Save the archive to the path provided.

Convert from RAR to ZIP

The provided code snippet demonstrates how to convert a RAR archive to a ZIP format in C#. It utilizes the Aspose.Zip library to easily extract the RAR entries and then create corresponding entries in the ZIP archive.

  using (Archive zip = new Archive())
  {
    using (RarArchive rar = new RarArchive("archive.rar"))
    {
      for (int i = 0; i < rar.Entries.Count; i++)
      {
        if (!rar.Entries[i].IsDirectory)
        {
          var ms = new MemoryStream();
          rar.Entries[i].Extract(ms);
          ms.Seek(0, SeekOrigin.Begin);
          zip.CreateEntry(rar.Entries[i].Name, ms);
        }
        else
          zip.CreateEntry(rar.Entries[i].Name + "/", Stream.Null);
      }
    }

    zip.Save("output.zip");
  }

Convert from ZIP to 7Z

The code snippet showcases the conversion of a ZIP archive to a 7Z format via C#. Leveraging the SevenZipArchive library, the script extracts the ZIP entries and creates corresponding entries in the 7Z archive.

Let’s review this sample step-by-step:

1. Instantiate 7z archive prepared for compressing. We’ll fill it with entries on next steps.
2. Instantiate ZIP archive prepared for decompression, supplying path to the archive.
3. Extract each entry to temporary storage.
4. Compose a 7z entry from that storage.
5. Save the archive to the path provided.

  using (SevenZipArchive sevenZipArchive =  new SevenZipArchive())
  {
    using (Archive archive = new Archive("source.zip"))
    {
      for (int i = 0; i < archive.Entries.Count; i++)
      {
        var ms = new MemoryStream();
        archive.Entries[i].Extract(ms);
        ms.Seek(0, SeekOrigin.Begin);
        sevenZipArchive.CreateEntry(archive.Entries[i].Name.Replace('\\', '/'), ms);
      }
    }

    sevenZipArchive.Save("output.7z");
  }

Convert from TAR.GZ file to ZIP

Here provided code snippet demonstrates the conversion of a TAR.GZ (GZIP) file to a ZIP format via C#. TarArchive class has convenient methods to operate with compressed tarball archives, particularly gzipped, the script extracts entries from the TAR.GZ file and creates corresponding entries in the ZIP archive.

using (Archive zip = new Archive())
{
  using (TarArchive archive = TarArchive.FromGZip("source.tar.gz"))
  {
  for (int i = 0; i < archive.Entries.Count; i++)
  {
    var ms = new MemoryStream();
    archive.Entries[i].Extract(ms);
    ms.Seek(0, SeekOrigin.Begin);
    zip.CreateEntry(archive.Entries[i].Name, ms);
    }
  }
  zip.Save("output.zip");
}

Conversion functions in the documentation

Aspose.Zip .NET API is known for its robust capability to handle complex archive conversions, allowing seamless transformation between various archive formats with utmost efficiency and reliability. With the ability to convert ZIP files to other popular archive formats and vice versa, developers can ensure data integrity and optimal performance across different platforms and applications. This essential conversion feature simplifies the process of data transfer, storage, and sharing, enabling smooth compatibility with diverse software environments. By leveraging the Aspose.Zip .NET API, users can effortlessly manipulate archive files and facilitate seamless integration with their desired applications, thus enhancing their productivity and workflow efficiency. The ZIP namespace contains classes which represent zip archive and common archive related entities.

Installing Aspose.Zip for .NET library

If you have used MSI installer to deploy Aspose.ZIP, follow these steps to completely remove the component and the associated demos and documentation:

  1. From the Start menu, select Settings followed by Control Panel.
  2. Click Add/Remove Programs.
  3. Select Aspose.ZIP.
  4. Click the Change/Remove button to remove Aspose.ZIP.

For more details about C# library installation, please refer to Aspose.ZIP Documentation .

Other Supported archive-file Conversions

You can also convert CAB, RAR, TAR, CPIO, TAR.BZ2, TAR.GZ, TAR.LZ, TAR.XZ, TAR.Z files into many other file formats including few listed below: