创建ZIP提取 [英] Creating a zip extractor

查看:334
本文介绍了创建ZIP提取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个zip提取为我创造了asp.net网站的插件。我希望客户机能够运行的提取器和将文件放置到我指定的正确的文件夹。是否有这样的任何工具?我不知道如何做到这一点,谢谢

I want to create a zip extractor for plugins that I create for asp.net sites. I want the client to be able to run the extractor and the files are placed into the correct folders that I specify. Are there any tools for this? I'm not sure how to do this, thanks

推荐答案

DotNetZip 是一个库,允许管理$ C $ Ç应用程序来读取或写入zip文件。有一件事它可以让你做的是产生一个自解压压缩文件(SFX)。

DotNetZip is a library that allows managed code apps to read or write zip files. One thing it allows you to do is produce a self-extracting archive (SFX).

在C#中,code,以产生一个自解压文件与DotNetZip看起来是这样的:

In C#, the code to produce an SFX archive with DotNetZip looks like this:

using (ZipFile zip1 = new ZipFile())
{
    // zip up a directory
    zip1.AddDirectory("C:\\project1\\datafiles", "data");
    zip1.Comment = "This will be embedded into a self-extracting exe";
    zip1.AddEntry("Readme.txt", "This is content for a 'Readme' file that will appear in the zip.");
    zip1.SaveSelfExtractor("archive.exe", SelfExtractorFlavor.WinFormsApplication);
}

您选择如何塑造ZIP文件的文件夹层次。唯一的规则是,在提取,提取发生于特定的根或父文件夹。 (你不能提取到散落的文件系统的7种不同的目录)

You choose how to shape the folder hierarchy in the ZIP file. The only rule is, upon extracting, the extraction happens to a particular root or parent folder. (You cannot extract to 7 different directories scattered about the filesystem)

由此产生的EXE需要.NET Framework 2.0或更高版本才能运行,但没有别的。

The resulting EXE requires the .NET Framework 2.0 or later in order to run, but nothing else.

在SFX / EXE也可以阅读和提取使用WinZip或其他压缩工具,包括Windows资源管理器的COM pressed文件夹一个普通的Zip文件。

The SFX/EXE is also a regular Zip file that you can read and extract with WinZip, or other zip tools, including Windows Explorer "Compressed Folders".

在SFX组成与其他压缩功能,包括

The SFX composes with the other zip features, including

  • 在WinZip的AES加密 - 这样你就可以加密的东西,并只允许开箱的人谁知道密码
  • ZIP64非常大的档案。
  • 在统一code,为正常〜ASCII范围之外的文件名。
  • 在恢复文件的属性,时间戳的等等。

在DotNetZip,有SFX两种可能的口味,你可以生成:一个控制台应用程序或WinForms应用程序。该UI的WinForms为您生成 - 没什么可写或设计。它的简单和实用,而且看起来是这样的:

In DotNetZip, there are two possible flavors of SFX that you can generate: a console app or a WinForms app. The WinForms UI is generated for you - nothing to write or design. It's simple and utilitarian, and looks like this:

控制台味道更适合在脚本被使用,或在无头(没有UI)情形。

The console flavor is more suited to being used in a script, or in a headless (no UI) scenario.


有一堆选项产生SFX的时候,这样的:

There are a bunch of options when producing the SFX, like:

  • 是否拆封后打开资源管理器
  • 在解压后执行(该命令是什么解包的一部分)的命令
  • 在命令'解包上执行选择性删除所有文件成功完成。尼斯的补丁程序。
  • 是否覆盖现有文件
  • Win32的图标,用于SFX EXE,或只接受默认
  • 默认提取目录,它可根据用户的环境,例如:%USERPROFILE%。
  • 在安静模式,这意味着它运行时,它提供或者没有UI可言,如果你要是使用的WinForms味道使用一个控制台应用程序,或者只是一个简单的进度条。有没有按钮点击 - 这只是SFX自动解压缩。

所有这些选项的SFX将如何表现是通过DotNetZip类库接口访问。

All these options for how the SFX will behave is accessible via the DotNetZip class library interface.


如果你不喜欢的UI或工作模式,内置的自解压功能,有一个简单的方法来提供自己的自解压UI +逻辑。例如,你可以在WPF中建立一个应用程序,让这一切时髦和视觉dyanmic。它的工作方式是pretty的简单:在产生SFX的code,复制你的解压存根EXE(的WinForms,WPF,或其他)输出流,然后在不关闭流,保存Zip文件到同一个流。它看起来像这样在VB:

If you don't like the UI or the working model for the built-in SFX capability, there's an easy way to provide your own self-extractor UI + logic. For example you could build an app in WPF and make it all snazzy and visually dyanmic. The way it works is pretty simple: in the code that produces the SFX, copy your unzip stub EXE (WinForms, WPF, or whatever) to an output stream, and then, without closing the stream, save the Zip file to the same stream. It looks like this in VB:

Public Shared Function Main(ByVal args As String()) As Integer
    Dim sfxStub As String = "my-sfx-stub.exe"
    Dim outputFile As String = "my-sfx-archive.exe"
    Dim directoryToZip As String = "c:\directory\to\ZIP"
    Dim buffer As Byte() = New Byte(4000){}
    Dim n As Integer = 1
    Using output As System.IO.Stream = File.Open(outputFile, FileMode.Create)
        '' copy the contents of the sfx stub to the output stream
        Using input As System.IO.Stream = File.Open(sfxStub, FileMode.Open)
            While n <> 0
                n = input.Read(buffer, 0, buffer.Length)
                If n <> 0 Then
                    output.Write(buffer, 0, n)
                End If
            End While
        End Using
        '' now save the zip file to the same stream
        Using zp As New ZipFile
            zp.AddFiles(Directory.GetFiles(directoryToZip), False, "")
            zp.Save(output)
        End Using
    End Using
End Function

本code创建生成的文件既是一个EXE和ZIP文件。存根EXE必须从自身的以阅读提取的。在上面的例子中,code为我-SFX-stub.exe程序可能是一些像这样简单:

The resulting file created by this code is both an EXE and a ZIP file. The stub EXE must read from itself in order to extract. In the example from above, the code for the my-sfx-stub.exe program could be something as simple as this:

Dim a As Assembly = Assembly.GetExecutingAssembly
Try
    '' read myself as a zip file
    Using zip As ZipFile = ZipFile.Read(a.Location)
        Dim entry As ZipEntry
        For Each entry in zip
            '' extract here, or add to a listBox, etc. 
            '' listBox1.Items.Add(entry.FileName)
        Next
    End Using
Catch
    MessageBox.Show("-No embedded zip file.-")
End Try

自定义SFX在这个简单的方法构建会对DotNetZip DLL的依赖。为了避免这种情况,并产生一个单一的自包含EXE,它能够在不另外DLL依赖提取,可以使用ILMerge,或者,<一href="http://stackoverflow.com/questions/96732/embedding-one-dll-inside-another-as-an-embedded-resource-and-then-calling-it-from">embed在DotNetZip DLL作为一种资源,并使用AssemblyResolver事件加载它从嵌入式资源的。

如果您有任何疑问,您可以要求在的DotNetZip论坛的。

If you have questions, you can ask on the DotNetZip forums.

这篇关于创建ZIP提取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆