开发者

ICSharpCode.SharpZipLib.Zip example with crc variable details

开发者 https://www.devze.com 2023-02-20 18:44 出处:网络
I am using icsharpziplib dll for zipping sharepoint files using c# in asp.net When i open the output.zip file, it is showing \"zip file is either corrupted or damaged\".

I am using icsharpziplib dll for zipping sharepoint files using c# in asp.net

When i open the output.zip file, it is showing "zip file is either corrupted or damaged". And the crc value for files in the outp开发者_运维知识库ut.zip is showing as 000000.

  1. How do we calculate or configure crc value using icsharpziplib dll?
  2. Can any one have the good example how to do zipping using memorystreams?


it seems you're not creating each ZipEntry.

Here's is a code that I adapted to my needs: http://wiki.sharpdevelop.net/SharpZipLib-Zip-Samples.ashx#Create_a_Zip_fromto_a_memory_stream_or_byte_array_1

Anyway with SharpZipLib there are many ways you can work with zip file: the ZipFile class, the ZipOutputStream and the FastZip.

I'm using the ZipOutputStream to create an in-memory ZIP file, adding in-memory streams to it and finally flushing to disk, and it's working quite good. Why ZipOutputStream? Because it's the only choice available if you want to specify a compression level and use Streams.

Good luck :)


1: You could do it manually but the ICSharpCode library will take care of it for you. Also something I've discovered: 'zip file is either corrupted or damaged' can also be a result of not adding your zip entry name correctly (such as an entry that sits in a chain of subfolders).

2: I solved this problem by creating a compressionHelper utility. I had to dynamically compose and return zip files. Temp files were not an option as the process was to be run by a webservice. The trick with this was a BeginZip(), AddEntry() and EndZip() methods (because I made it into a utility to be invoked. You could just use the code directly if need be).

Something I've excluded from the example are checks for initialization (like calling EndZip() first by mistake) and proper disposal code (best to implement IDisposable and close your zipfileStream and your memoryStream if applicable).

using System.IO;
using ICSharpCode.SharpZipLib.Zip;

public void BeginZipUpdate()
    {
        _memoryStream = new MemoryStream(200);
        _zipOutputStream = new ZipOutputStream(_memoryStream);
    }

public void EndZipUpdate()
    {
        _zipOutputStream.Finish();
        _zipOutputStream.Close();
        _zipOutputStream = null;
    }

//Entry name could be 'somefile.txt' or 'Assemblies\MyAssembly.dll' to indicate a folder.
//Unsure where you'd be getting your file, I'm reading the data from the database.
public void AddEntry(string entryName, byte[] bytes)
    {
        ZipEntry entry = new ZipEntry(entryName);
        entry.DateTime = DateTime.Now;            
        entry.Size = bytes.Length;            
        _zipOutputStream.PutNextEntry(entry);
        _zipOutputStream.Write(bytes, 0, bytes.Length);
        _zipOutputStreamEntries.Add(entryName);
    }

So you're actually having the zipOutputStream write to a memoryStream. Then once _zipOutputStream is closed, you can return the contents of the memoryStream.

public byte[] GetResultingZipFile()
    {           
        _zipOutputStream.Finish();
        _zipOutputStream.Close();
        _zipOutputStream = null;
        return _memoryStream.ToArray();
    }

Just be aware of how much you want to add to a zipfile (delay in process/IO/timeouts etc).

0

精彩评论

暂无评论...
验证码 换一张
取 消