简体   繁体   中英

It does not appear to be a valid archive (Byte array to zip file in c#.net)

I am facing issue while converting byte array to zip file.Even though zip file is created using the below code but when I am extracting the zip file I am getting error "Cannot open file. It does not appear to be a valid archive" .

private static void ShowZipFile(string fileName, byte[] data)
{
    byte[] compress = Compress(data);
    File.WriteAllBytes(fileName, compress);
}            

private static byte[] Compress(byte[] data)
{
    using (MemoryStream memory = new MemoryStream())
    {
        using (GZipStream gzip = new GZipStream(memory,
        CompressionMode.Compress, true))
        {
            gzip.Write(data, 0, data.Length);
        }
        return memory.ToArray();
    } 

}

A GZipStream isn't a zip file, basically - it's a gzip file. That's just compressed data, without any notions of multiple files, file names etc. If you save the file as foo.gz you may find that the zip tool you use knows how to decompress that, but you definitely need to understand that it's not the same as a foo.zip with file entries etc.

If you want to create an actual zip file, you might want to look at SharpZipLib , System.IO.Compression.ZipFile or similar libraries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM