Richard Griffiths
Richard Griffiths

Reputation: 838

Zipfile in vb.net 4.5 "The directory name is invalid." error - can't identify the cause?

I've got a small utility that can encrypt/decrypt using .net 4.5 TripleDES routines. I added compression and found the simpler compression stuff doesn't work for me.

The files referenced in these fields definitely exist, aren't huge at all and are even named in the same case.

Yet I keep getting "The directory name is invalid.". If I take out the test for existence of the archive, I get a different error - as it's made the zip and it's 0 bytes. I've searched for far longer on this than it took to work out how to use the encryption part of my utility yesterday - and that's with learning how streams work!

Can anyone shed light on this issue at all please?

Private Encoded As String = "C:\TestFile\encoded.txt"
Private Decoded As String = "C:\TestFile\decoded.txt"
Private CompressEncoded As String = "C:\TestFile\encoded.zip"

Private Sub Main()
  Compress(Encoded, CompressEncoded)
End sub

Sub Compress(filename As String, zippedFile As String)
'Added to handle file already exists error
  If IO.File.Exists(zippedFile) Then IO.File.Delete(zippedFile)
  If IO.File.Exists(filename) Then IO.Compression.ZipFile.CreateFromDirectory(filename, zippedFile, CompressionLevel.Fastest, True)
End Sub

In addition to this code I also tried adding a quick streamreader test to prove to myself that filename does exist. ' Dim sr As New StreamReader(filename) MessageBox.Show(sr.ReadToEnd)

It does and displays the line of text within it.

I'd be grateful for any help - this rather silly bug has chewed up lots of time this afternoon that I was hoping to use on something more useful :).

Thanks folks!

Thanks for the answer!

Sub Compress(filename As String, zippedFile As String)
    If IO.File.Exists(zippedFile) Then IO.File.Delete(zippedFile)

    If IO.File.Exists(filename) Then
        Using archive As ZipArchive = Open(zippedFile, ZipArchiveMode.Create)
            archive.CreateEntryFromFile(filename, Path.GetFileName(filename), CompressionLevel.Fastest)
        End Using

    End If
End Sub

Sub Decompress(ZippedFile As String, UnzippedFile As String)
    If IO.File.Exists(UnzippedFile) Then IO.File.Delete(UnzippedFile)

    If IO.File.Exists(ZippedFile) Then
        Using archive As ZipArchive = Open(ZippedFile, ZipArchiveMode.Read)
            archive.ExtractToDirectory(Path.GetDirectoryName(UnzippedFile))
        End Using
    End If
End Sub

Upvotes: 2

Views: 4712

Answers (2)

ajakblackgoat
ajakblackgoat

Reputation: 2149

If you want to compress a single file, use ZipArchive class like below:

Sub Compress(filename As String, zippedFile As String)
    If IO.File.Exists(zippedFile) Then IO.File.Delete(zippedFile)

    If IO.File.Exists(filename) Then
        Using archive As ZipArchive = ZipFile.Open(zippedFile, ZipArchiveMode.Create)
            archive.CreateEntryFromFile(filename, Path.GetFileName(filename), CompressionLevel.Fastest)
        End Using

    End If
End Sub

ADDITIONAL INFO

Need to add reference to System.IO.Compression.dll for the ZipArchive and System.IO.Compression.FileSystem.dll for the ZipFile class. These DLLs are not referenced by default when a project is created in VS.

Upvotes: 2

Steve
Steve

Reputation: 216293

Probably it is a simple error.
The first parameter to pass to CreateFromDirectory should be a directory name, but you pass a file name.

Try with

 If IO.File.Exists(filename) Then 
      IO.Compression.ZipFile.CreateFromDirectory(Path.GetDirectoryName(filename), _
                        zippedFile, CompressionLevel.Fastest, True)
 End If

Upvotes: 3

Related Questions