如何使用SevenZipSharp创建压缩的SFX文件?

时间:2013-04-17 20:17:46

标签: .net vb.net visual-studio 7zip sevenzipsharp

我将了解如何使用SevenZipSharp库创建SFX。

首先,我需要说我找不到任何属性来设置压缩级别,以及所有这些。

当我尝试制作文件的SFX时,我收到此错误:

"Object reference not set to an instance of an object."

如果我尝试制作文件夹的SFX,我会收到此错误:

"Access to the path 'C:\test' is denied."

(但不是真的,我是管理员,我用更多可用的文件夹对它进行了测试......)

这是我正试图理解所有这一切的完整课程......:

Imports SevenZip

Public Class Form1

Dim dll As String = "7z64.dll"

Private Function SevenZipSharp_Compress_SFX(ByVal Input_DirOrFile As String, _
                                            ByVal OutputFileName As String) As Boolean
    Try
        ' Set library path
        SevenZipCompressor.SetLibraryPath(dll)

        ' Create compressor
        Dim Compressor As SevenZipSfx = New SevenZipSfx(SfxModule.Default)

        ' Set SFX parameters
        ' ¿?

        ' Start compression
        Compressor.MakeSfx(Input_DirOrFile, OutputFileName)

    Catch ex As Exception
        'Return False ' File not compressed
        Throw New Exception(ex.Message)
    End Try

    Return True ' File compressed

End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    SevenZipSharp_Compress_SFX("C:\test\file.bat", "C:\7zSFX.exe")
End Sub

End Class
  

更新:

@For Everyone:

请为那些将回答我的问题的人祈祷,至少你曾经创建过一个SFX SevenZipSharp来告诉我我做错了什么以及如何修复它,而不回答说它们是用户许可问题,请阅读评论。

2 个答案:

答案 0 :(得分:2)

您可能正在使用Windows 8,因此为了使您的应用程序具有在(C :)分区中进行写入修改的足够权限,您应该以“以管理员身份”模式运行应用程序,即使您是管理员。 / p>

答案 1 :(得分:2)

看起来可能存在一些关于论据应该是什么的混淆。以下代码适用于codeplex上最新的SevenZipSharp代码。

    Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim compressor As SevenZipSfx = New SevenZipSfx("7z.sfx")
        compressor.MakeSfx("C:\Temp\cc_seal.7z", "C:\Temp\sfxseal.exe")
    End Sub

我尝试使用SevenZipSfx(SfxModule.Default),就像你的例子一样,但模块名称没有设置,我相信“对象引用没有设置为对象的实例”错误来自,因为我这样做了:

    Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim compressor As SevenZipSfx = New SevenZipSfx(SfxModule.Default)
        compressor.ModuleFileName = "7z.sfx"
        compressor.MakeSfx("C:\Temp\cc_seal.7z", "C:\Temp\sfxseal.exe")
    End Sub

它也没有错误地为我工作。取出ModuleFileName行,我遇到了和你一样的崩溃。

另请注意,compress.MakeSfx的第一个参数需要是.7z文件,而不是.bat文件。它会“工作”,但是当你尝试运行sfx.exe时,它会崩溃,因为它不是一个有效的7zip文件。因此,您需要先压缩文件/目录。

确保7z.sfx在您的应用程序目录中,或提供它的路径(它在codeplex源代码下载)

我首先使用“7zxSD_All.sfx”文件尝试了它,它提取文件然后Windows 7给出了关于它没有正确安装的错误(我假设Windows 7认为它是安装文件而不是自解压文件)。 “7z.sfx”虽然奏效了。

相关问题