ComponentOne Zip for .NET
Zip for .NET Task-Based Help / Extracting Files from Zip Entry to Memory
In This Topic
    Extracting Files from Zip Entry to Memory
    In This Topic

    To extract a file from a zip to memory variable (for instance, a Byte array), use the following function:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    Private Function GetDataFromZipFile(zipFileName As String, entryName As String) As Byte()
         ' Get the entry from the zip file.
        Dim zip As New C1ZipFile()
        zip.Open(zipFileName)
        Dim ze As C1ZipEntry = zip.Entries(entryName)
         ' Copy the entry data into a memory stream.
        Dim ms As New MemoryStream()
        Dim buf(1000) As Byte
        Dim s As Stream = ze.OpenReader()
        Try
            While True
                Dim read As Integer = s.Read(buf, 0, buf.Length)
                If read = 0 Then
                    Exit While
                End If
                ms.Write(buf, 0, read)
            End While
        Finally
            s.Dispose()
        End Try
        s.Close()
         ' Return result.
        Return ms.ToArray()
    End Function
    

    To write code in C#

    C#
    Copy Code
    private byte[] GetDataFromZipFile(string zipFileName, string entryName)
    {
           // Get the entry from the zip file.
        C1ZipFile zip = new C1ZipFile();
        zip.Open(zipFileName);
        C1ZipEntry ze = zip.Entries[entryName];
           // Copy the entry data into a memory stream.
        MemoryStream ms = new MemoryStream();
        byte[] buf = new byte[1000];
        using (Stream s = ze.OpenReader())
        {
            for (;;)
            {
                int read = s.Read(buf, 0, buf.Length);
                if (read == 0) break;
                ms.Write(buf, 0, read);
            }
        }
          // There's no need to call close because of the C# 'using' 
          // statement above but in VB this would be necessary.
          //s.Close();
           // Return result.
        return ms.ToArray();
    }