【问题标题】:How to write to add files to a zip archive using VB.NET code equivalent to VbScript?如何使用等效于 VbScript 的 VB.NET 代码编写文件以将文件添加到 zip 存档?
【发布时间】:2014-09-18 03:05:21
【问题描述】:

以下 vbscript 代码将文件夹的内容添加到 zip 存档中:

Set objArgs = WScript.Arguments
folder = objArgs(0)
zip = objArgs(1)

CreateObject("Scripting.FileSystemObject").CreateTextFile(zip, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)

Set objShell = CreateObject("Shell.Application")
Set source = objShell.NameSpace(folder).Items
objShell.NameSpace(zip).CopyHere(source)

wScript.Sleep 5000

然而,当我尝试使用等效的 VB.NET 代码时,我总是得到一个空的 zip 文档。下面的代码做错了什么?

Const folder As String = "C:\temp"
Const zip As String = "C:\output.zip"

CreateObject("Scripting.FileSystemObject").CreateTextFile(zip, True).Write("PK" & Chr(5) & Chr(6) & New String(Chr(65), 18).Replace(Chr(65), Chr(0))) 'New String(vbNullChar, 18))

Dim objShell As Object = CreateObject("Shell.Application")
Dim source As Object = objShell.NameSpace(folder).Items
objShell.NameSpace(zip).CopyHere(source)

Sleep(5000)

... 声明 Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Integer)

【问题讨论】:

标签: vb.net vbscript


【解决方案1】:

在 (vb).Net 中有一个非常好的用于写入 zip 文件的库。 http://dotnetzip.codeplex.com/

Dim z As New Ionic.Zip.ZipFile("File name of zip file")
z.AddDirectory("Path to directory to add")
z.save()

【讨论】:

  • 要求代码远离第三方库。感谢您的建议
【解决方案2】:

如果您使用的是 (.NET 2) 或 (.NET 4),请尝试 (GZipStream): http://msdn.microsoft.com/en-us/library/system.io.compression.gzipstream%28v=vs.80%29.aspx

如果您使用的是 (.NET 4.5),请使用 (ZipArchive): http://msdn.microsoft.com/en-us/library/system.io.compression.ziparchive%28v=vs.110%29.aspx

【讨论】:

    【解决方案3】:

    流动的代码对我有用

    这是原始网址 http://www.codeproject.com/Tips/257193/Easily-Zip-Unzip-Files-using-Windows-Shell

    代码:

    Dim outputZip As String = "output zip file path"
    Dim inputZip As String = "input zip file path"
    Dim inputFolder As String = "input folder path"
    Dim outputFolder As String = "output folder path"
    
    'Declare the shell object
    Dim shObj As Object = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"))
    
    Sub Zip()
    
        'Lets create an empty Zip File .
        'The following data represents an empty zip file.
        Dim startBytes() As Byte = {80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, _
                                     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
        'Data for an empty zip file.
    
        FileIO.FileSystem.WriteAllBytes(outputZip, startBytes, False)
        'We have successfully created the empty zip file.
    
        'Declare the folder which contains the items (files/folders) that you want to zip.
        Dim input As Object = shObj.NameSpace((inputFolder))
    
        'Declare the created empty zip file.
        Dim output As Object = shObj.NameSpace((outputZip))
    
        'Compress the items into the zip file.
        output.CopyHere((input.Items), 4)
    
    End Sub
    

    问候

    【讨论】:

      猜你喜欢
      • 2011-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-15
      相关资源
      最近更新 更多