Results 1 to 8 of 8

Thread: [RESOLVED] Compressing files

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    South Africa
    Posts
    141

    Resolved [RESOLVED] Compressing files

    If have the following code to compress a .csv file into a .zip file :

    Code:
    Imports System
    Imports System.IO.Compression
    
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
    
            Dim startPath As String = "D:\Wingerdbou\LutzvilleProgram\InvoerDataFiles\EZY Wine\Vessel_Composition\compositionVIRZIP.csv"
            Dim zipPath As String = "D:\Wingerdbou\LutzvilleProgram\InvoerDataFiles\EZY Wine\Zipped\test.zip"
            Dim extractPath As String = "D:\Wingerdbou\LutzvilleProgram\InvoerDataFiles\EZY Wine\Unzipped"
    
            ZipFile.CreateFromDirectory(startPath, zipPath)
    
            ZipFile.ExtractToDirectory(zipPath, extractPath)
    
            MsgBox("Done")
        End Sub
    
    End Class




    But I get the following error : System.IO.IOException: 'The parameter is incorrect. : 'D:\Wingerdbou\LutzvilleProgram\InvoerDataFiles\EZY Wine\Vessel_Composition\compositionVIRZIP.csv''


    From https://docs.microsoft.com/en- I can see a @ is placed before the file description, but how to do it in VB.Net?

    I have both System.IO.Compression and System.IO.Compression.ZipFile installed via NuGet.

    I am using .NET 5.0

    Regards

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Compressing files

    Firstly, the @ symbol is irrelevant to VB. That indicates a verbatim string literal in C#, which means that the backslash character is considered to be a literal character like any other, rather than an escape character. Basically, instead of doing this:
    csharp Code:
    1. var filePath = "C:\\folder\\subfolder\\file.ext";
    you can do this:
    csharp Code:
    1. var filePath = @"C:\folder\subfolder\file.ext";
    The backslash character isn't an escape character in VB to begin with, so there's no need to specify that it's not.

    Secondly, you should not have seen that in the first place when reading the Microsoft Docs site. That site defaults to C# code examples but you can select a different language at the top of any page and that will persist whenever you use the site. If you select VB there then you will always see VB examples whenever they are available.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Compressing files

    As for the issue, if you are calling a method named ZipFile.CreateFromDirectory then what do you think the path you pass it as the source should refer to? Probably the directory you want to create the ZIP file from, right? The parameter is even named sourceDirectoryPath. Is that what you're doing? No, it is not. You're passing the path of a file, which is not a directory, thus that parameter is incorrect.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Compressing files

    By the way, I just tested code like yours in a VB WinForms .NET 5.0 app and I didn't have to add any NuGet packages. Microsoft.NETCore.App was listed under Dependencies\Frameworks and System.IO.Compression.ZipFile was listed under that. I know that you can specify that a .NET Core app relies on an installed framework or brings its dependencies with it but I haven't played around with that as yet. Did you specify that your app would provide its own dependencies specifically?

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    South Africa
    Posts
    141

    Re: Compressing files

    Thank you. Especially for the tip on Microsoft docs.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    South Africa
    Posts
    141

    Re: Compressing files

    Thank you jmchilhinney. Yes I was incorrectly passing a file in stead of a directory.
    From what I read I then take it one cannot compress a single file in a directory. Everything gets compressed into a archive. From this archive one then can use the ZipArcive.GetEntry to extract a single file. My problem is that I need to create a .zip file on a daily basis - so I want to keep the zipped archive as small as possible.
    I guess I have to make sure only a single .csv file is in the directory?

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Compressing files

    Of course you can create a ZIP file from a single file, but it's more than a single method call. I haven't tried to do it but I think that you'd call ZipFile.Open to Create the new ZIP file, then call the CreateEntryFromFile extension method on the returned ZipArchive.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Nov 2016
    Location
    South Africa
    Posts
    141

    Re: Compressing files

    Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width