Question

I was trying to make a program that zips a folder but I keep getting a NullReferenceException and it does not create the required zip file. Have I forgot something? I am using Shell32 and the zip sub comes from a codeproject tutorial. Anyways, here is the code:

Imports System.IO
Imports Shell32
Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.Hide()
    Me.ShowInTaskbar = False
    Zip()
End Sub
Sub Zip()
    Dim sc As New Shell32.Shell()
    Dim input As Shell32.Folder = sc.NameSpace("C:\minercraft v2.0\")
    Dim output As Shell32.Folder = sc.NameSpace("C:\minercraft v2.0\backup.zip")
    output.CopyHere(input.Items, 4)
End Sub

End Class
Was it helpful?

Solution

Here is an example that worked for me: Tweak your paths as necessary. The trick is that you have to write out a blank file first.

see: http://www.codeproject.com/Tips/257193/Easily-zip-unzip-files-using-Windows-Shell32

  Dim sc As New Shell32.Shell()
  Dim startBuffer() 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("C:\backup.zip", startBuffer, False)
  Dim input As Shell32.Folder = sc.NameSpace("C:\temp")
  Dim output As Shell32.Folder = sc.NameSpace("C:\backup.zip")
  output.CopyHere(input.Items, 4)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top