问题
I will understand how to create a SFX using SevenZipSharp library.
First of all I need to say I can't find any property to set the compression level, and all of that.
And when I try to make an SFX of a file, I get this error:
"Object reference not set to an instance of an object."
If I try to make an SFX of a folder, I get this error:
"Access to the path 'C:\test' is denied."
(But is not True, I'm Admin and I've tested it with more avaliable folders...)
This is the full class where I'm trying to understand all of this... :
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
UPDATE:
@For Everyone:
please I pray to someone who will answer my question at least you used to create a SFX SevenZipSharp to tell me what I'm doing wrong and how to fix it, not to answer to say that they are user permission issues, please read the comments.
回答1:
It looks like there may be some confusion as to what the arguments should be. The following code worked for me with the latest SevenZipSharp code on codeplex.
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
I tried with the SevenZipSfx(SfxModule.Default) like in your example, but the module name wasn’t being set and I believe that’s where the “Object reference not set to an instance of an object” error was coming from, because I did this:
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
And it also worked for me without error. Take out the ModuleFileName line, and I got the same crash as you did.
Also notice that compressor.MakeSfx first argument needs to be a .7z file and not a .bat file. It will “work” but when you try to run the sfx.exe it will crash with something about it not being a valid 7zip file. So you'll need to compress your file/directory first.
Make sure 7z.sfx in in your application directory, or provide the path to it (it’s in the codeplex source download)
I tried it with the “7zxSD_All.sfx” file first, it extracts the file then Windows 7 gives an error about it not being installed correctly (I'm assuming Windows 7 thinks it's an install file and not a self extracting file). "7z.sfx" worked though.
回答2:
You are probably using Windows 8, so in order to give your application the sufficient privileges to write-modify in (C:) partition, you should run the application in "As Administrator" mode even you're the admin.
来源:https://stackoverflow.com/questions/16069470/how-to-create-a-compressed-sfx-file-using-sevenzipsharp