Wai Wai Han
Wai Wai Han

Reputation: 11

How to read zip file content?

How to read the content of file from zip without decompressing ? After searching the file name in zip ,I want to extract the file in window temp folder and copy the file and then delete extract file. Please reply for my question .

Upvotes: 1

Views: 748

Answers (1)

Francesco Baruchelli
Francesco Baruchelli

Reputation: 7468

You can use sharpziplib to read the file without writing it to disk. It can be done like this:

    public string Uncompress(string zipFile, string entryName)
    {
        string s = string.Empty;
        byte[] bBuffer = new byte[4096];
        ZipInputStream aZipInputStream = null;

        aZipInputStream = new ZipInputStream(File.OpenRead(zipFile));
        ZipEntry anEntry;
        while ((anEntry = aZipInputStream.GetNextEntry()) != null)
        {
            if (anEntry.Name == entryName)
            {
                MemoryStream aMemStream = new MemoryStream();
                int bSize;
                do
                {
                    bSize = aZipInputStream.Read(bBuffer, 0, bBuffer.Length);
                    aMemStream.Write(bBuffer, 0, bSize);
                }
                while (bSize > 0);
                aMemStream.Close();
                byte[] b = aMemStream.ToArray();
                s = Encoding.UTF8.GetString(b);
                aZipInputStream.CloseEntry();
                break;
            }
            else
                aZipInputStream.CloseEntry();
        }
        if (aZipInputStream != null)
            aZipInputStream.Close();
        return s;
    }

Upvotes: 3

Related Questions