带有 crc 变量详细信息的 ICSharpCode.SharpZipLib.Zip 示例

发布于 2024-10-28 15:50:12 字数 263 浏览 7 评论 0原文

我正在使用 icsharpziplib dll 在 asp.net 中使用 c# 来压缩共享点文件
当我打开 output.zip 文件时,它显示“zip 文件已损坏或损坏”。 Output.zip 中文件的 crc 值显示为 000000。

  1. 我们如何使用 icsharpziplib dll 计算或配置 crc 值
  2. 任何人都可以提供一个很好的示例如何使用内存流进行压缩吗?

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 output.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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愁杀 2024-11-04 15:50:12

看来您没有创建每个 ZipEntry。

这是我根据我的需要进行调整的代码:
http://wiki.sharpdevelop.net/SharpZipLib-Zip-Samples.ashx#Create_a_Zip_fromto_a_memory_stream_or_byte_array_1

无论如何,使用 SharpZipLib,您可以通过多种方式处理 zip 文件:ZipFile 类、ZipOutputStreamFastZip

我使用 ZipOutputStream 创建内存中 ZIP 文件,向其中添加内存流,最后刷新到磁盘,效果非常好。为什么选择 ZipOutputStream?因为如果您想指定压缩级别并使用 Streams,这是唯一可用的选择。

祝你好运 :)

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 :)

一向肩并 2024-11-04 15:50:12

1:
可以手动完成,但 ICSharpCode 库会为您处理。我还发现:“zip 文件已损坏或损坏”也可能是由于未正确添加 zip 条目名称(例如位于子文件夹链中的条目)造成的。

2:
我通过创建压缩助手实用程序解决了这个问题。我必须动态地编写并返回 zip 文件。临时文件不是一个选项,因为该进程将由 Web 服务运行。
这样做的技巧是 BeginZip()、AddEntry() 和 EndZip() 方法(因为我将其制作成要调用的实用程序。如果需要,您可以直接使用代码)。

我从示例中排除的内容是初始化检查(例如首先错误地调用 EndZip())和正确的处理代码(最好实现 IDisposable 并关闭您的 zipfileStream 和内存流(如果适用))。

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);
    }

因此,您实际上是将 zipOutputStream 写入内存流。然后一旦 _zipOutputStream 关​​闭,就可以返回内存流的内容。

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

只需要注意您想要向 zip 文件添加多少内容(进程延迟/IO/超时等)。

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文