简体   繁体   English

如何在.net中编写我自己的zip类

[英]How to Write my own zip class in .net

I wanted to use .net's System.IO.Compression.GZipStream to compress and decompress a directory. 我想使用.net的System.IO.Compression.GZipStream压缩和解压缩目录。 But I find it only able to compress a single file, how can I write a zip class that can compress a specific directory to a zip file? 但是我发现它只能压缩单个文件,如何编写一个可以将特定目录压缩为zip文件的zip类?

ps: we're not allowed to use any third party zip library ps:我们不允许使用任何第三方zip库

You should TAR the files first. 您应该先对文件进行TAR。

EDIT 编辑

If you are feeling adventurous why don't you design your own header and write each zipped file sequentially. 如果您喜欢冒险,为什么不设计自己的标题并依次写入每个压缩文件。 Store all the file names in the header and the index of where they start. 将所有文件名存储在标题和它们开始的索引中。

There is plenty of documentation on the .zip file format. 有关.zip文件格式的大量文档。 Wikipedia is a good place to start: http://en.wikipedia.org/wiki/ZIP_file_format . Wikipedia是一个不错的起点: http : //en.wikipedia.org/wiki/ZIP_file_format

You can use the System.IO.Compression.DeflateStream for the actual compression, but you'll need a CRC32 implementation, and you'll need to layout the file with the correct headers, etc. 您可以使用System.IO.Compression.DeflateStream进行实际的压缩,但是需要CRC32实现,并且需要使用正确的标头对文件进行布局等。

It is a non-trivial amount of work. 这是一项艰巨的工作。

You might consider purchasing a commercial .zip library if you are unable to use an open-source library. 如果您无法使用开源库,则可以考虑购买商业.zip库。 You should not have any legal issues with using a commercial library**, and the cost to purchase a license is almost certainly cheaper than the cost of developing and testing the library yourself. 使用商业图书馆**应该不会有任何法律问题,而且购买许可证的费用几乎肯定比自己开发和测试该图书馆的费用便宜。 Also, check around. 另外,检查一下。 Your company might have a license for a commercial library already. 您的公司可能已经拥有商业图书馆的许可证。

**I'm not a lawyer, so check with your legal contact. **我不是律师,因此请与您的法律联系人联系。

如果您使用的是3.0+版本,则可以使用System.IO.Packaging创建zip文件。

Use System.IO.Directory and System.IO.File classes to enumerate all the files and folders, do it recursively to ensure you get file in subfolders. 使用System.IO.Directory和System.IO.File类枚举所有文件和文件夹,以递归方式进行操作以确保在子文件夹中获取文件。 Once you have all the files zip them one at a time (or use a threadpool) and you should have what you need. 一旦所有文件都一次压缩一个(或使用线程池),就应该拥有所需的文件。

You have a few options 您有几种选择

1. Use Shell.Application 1.使用Shell.Application
You can call out to the Shell. 您可以呼唤Shell。 In VBScript it looks like this: 在VBScript中,它看起来像这样:

dim sa
set sa = CreateObject("Shell.Application")

Dim zip
Set zip = sa.NameSpace(pathToZipFile)

Dim d
Set d = sa.NameSpace(dirToZip)


For Each s In d.items
    WScript.Echo  s
Next


' http://msdn.microsoft.com/en-us/library/bb787866(VS.85).aspx
' ===============================================================
' 2nd arg is a bit field: 
' 4 = do not display a progress box
' 16 = Respond with "Yes to All" for any dialog box that is displayed.
' 128 = Perform the operation on files only if a wildcard file name (*.*) is specified. 
' 256 = Display a progress dialog box but do not show the file names.
' 2048 = Version 4.71. Do not copy the security attributes of the file.
' 4096 = Only operate in the local directory. Don't operate recursively into subdirectories.

WScript.Echo "copying files..."

zip.CopyHere d.items, 4

' CopyHere is asynchronous. Gotta wait til it's done. 
sLoop = 0
Do Until d.Items.Count <= zip.Items.Count
    Wscript.Sleep(1000)
Loop

I don't know if this is accessible from managed code, but I would guess so. 我不知道这是否可以从托管代码访问,但是我猜想是这样。 It feels sort of hacky to me though. 不过,这对我来说有点不客气。

Also, you need to pre-initialize the zipfile before adding things to it: 另外,您需要在将文件添加到其中之前预先初始化zipfile:

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Dim file
Set file = fso.CreateTextFile(pathToZipFile)

file.Write Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, 0)

file.Close
Set fso = Nothing
Set file = Nothing

2. Implement it yourself 2.自己实施
If you don't want to use Shell.Application, in theory you could create your own zip class implementation without too much trouble, especially if you have limited requirements. 如果您不想使用Shell.Application,从理论上讲,您可以创建自己的zip类实现,而不会遇到太多麻烦,尤其是在需求有限的情况下。 The zip format is not super complicated, if you don't implement encryption, zip64, Unicode, high-resolution timestamps, streaming, and so on... 如果您未实现加密,zip64,Unicode,高分辨率时间戳,流式传输等,则zip格式并不复杂。

3. Do the legal review, and Re-Use 3.进行法律审查,然后重新使用
Rather than spending engineering time developing a new implementation, investigate the implementations that are out there now. 与其花费工程时间来开发新的实现,不如研究目前存在的实现。 Some people worry about the GPL with SharpZipLib. 有些人担心SharpZipLib的GPL。 DotNetZip is released under the Microsoft Public License, very permissive and no viral terms. DotNetZip是根据Microsoft公共许可发布的,非常宽松,没有任何病毒性条款。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何用.NET编写我自己的Global Snippets程序? - How to write my own Global Snippets program with .NET? 如何编写我自己的AuthorizeTag? - How to write my own AuthorizeTag? 如何编写自己的.net混淆器 - How to write your own .net obfuscator 我将如何编写自己的FirstOrDefaultAsync()方法 - How would I write my own FirstOrDefaultAsync() method 如何在VB.NET中创建自己的组件? - How can I make my own components in VB.NET? 如何在.NET中创建自己的原始数据类型? - How to I create my own primitive data type in .NET? 如何禁用.NET Framework异常处理并使用我自己的异常处理? - How to disable .NET Framework exception handling and use my own instead? 如何使用.net框架创建自己的报告控件? - How to create my own reporting controls with .net framework? 如何最好地用C#4编写自己的后台工作和可交流的(发送进度更新并获取消息)非可视类? - How do I best write my own background-working and communicatible (sending progress updates and getting messages) non-visual class in C# 4? 如何将类添加到我自己的控制台应用程序项目中 - How can I add a class to my own console app project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM