locked
vb.net Zip from files RRS feed

  • Question

  • Hi everybody,

    I working on a zip module who will zip all logs of an application.

    so It should be easy to have just to  zip the complete folder, but, I have to let the choice to  the user which kind of log he want to save. So I created a list of file I want to save.

    also I have to save the log file of the tool which start the zipping, so it's refused because the file is open.

    Could you please help me on this, I'm a bit stuck with it ?

    When I open my zip file I also receive warning messages saying the files are Read-only

    Thanks

    _listOfFiles is my list of the file I want to zip

    Using archive As ZipArchive = System.IO.Compression.ZipFile.Open(strZipFile, ZipArchiveMode.Update)
                    For Each logFile As String In _listOfFiles
                        archive.CreateEntryFromFile(logFile, logFile)
                        'Dim FileToAdd As ZipArchiveEntry = archive.CreateEntry(logFile)
                        'Using writer As StreamWriter = New StreamWriter(FileToAdd.Open())
    
                        'End Using
    
                    Next
    End Using


    Monday, March 30, 2015 2:12 PM

Answers

  • This zip the file C:\tst\Cat.png  .. and add it to an existing ZipFile C:\tst\MyZipFile.zip

    The error you are getting is that the second argument , the name of the archive, cannot contain any slashes. It must be the file name without the directory path

        Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
            Dim FileToAdd As String = "c:\tst\Cat.png"
    
            Using zipToOpen As IO.FileStream = New IO.FileStream("c:\tst\MyZipFile.zip", FileMode.OpenOrCreate)
                Using archive As ZipArchive = New ZipArchive(zipToOpen, ZipArchiveMode.Update)
                    archive.CreateEntryFromFile(FileToAdd, IO.Path.GetFileName(FileToAdd), CompressionLevel.Optimal)
                End Using
            End Using
    
        End Sub



    • Edited by Crazypennie Thursday, April 2, 2015 10:12 AM
    • Proposed as answer by Armin Zingler Thursday, April 2, 2015 11:47 AM
    • Marked as answer by Carl Cai Tuesday, April 7, 2015 6:36 AM
    Monday, March 30, 2015 5:35 PM

All replies

  • This zip the file C:\tst\Cat.png  .. and add it to an existing ZipFile C:\tst\MyZipFile.zip

    The error you are getting is that the second argument , the name of the archive, cannot contain any slashes. It must be the file name without the directory path

        Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
            Dim FileToAdd As String = "c:\tst\Cat.png"
    
            Using zipToOpen As IO.FileStream = New IO.FileStream("c:\tst\MyZipFile.zip", FileMode.OpenOrCreate)
                Using archive As ZipArchive = New ZipArchive(zipToOpen, ZipArchiveMode.Update)
                    archive.CreateEntryFromFile(FileToAdd, IO.Path.GetFileName(FileToAdd), CompressionLevel.Optimal)
                End Using
            End Using
    
        End Sub



    • Edited by Crazypennie Thursday, April 2, 2015 10:12 AM
    • Proposed as answer by Armin Zingler Thursday, April 2, 2015 11:47 AM
    • Marked as answer by Carl Cai Tuesday, April 7, 2015 6:36 AM
    Monday, March 30, 2015 5:35 PM
  • Thanks, indead it's now working fine thanks guy
    Thursday, April 2, 2015 9:42 AM
  • Thanks, indead it's now working fine thanks guy

    Then you should propose the post that answered your question as the answer.

    La vida loca

    Thursday, April 2, 2015 12:05 PM