This is a migrated thread and some comments may be shown as answers.

Converting from ZipPackage to ZipArchive

3 Answers 190 Views
ZipLibrary
This is a migrated thread and some comments may be shown as answers.
Charles
Top achievements
Rank 1
Charles asked on 28 Aug 2015, 06:23 PM

I am trying to incorporate the latest build with ZipArchive rather than ZipPackage and am having difficulty getting it to create a valid zip file. Here is what works using the ZipPackage:

Dim VehicleID As Int32 = CInt(ddVehicleList.SelectedValue)
 
            Dim memStream As New MemoryStream()
            Dim Package As ZipPackage = ZipPackage.Create(memStream)
 
            If VehicleID <> 0 Then
                Dim fileIndex As Int32 = 0
                For Each lvItem As ListViewItem In lvImages.Items
                    Try
                        Dim cbSelected As CheckBox = lvItem.FindControl("cbSelected")
                        If cbSelected.Checked Then
                            fileIndex += 1
                            Dim url As String = cbSelected.Attributes("url")
                             
                            Dim photo As ZipData.Photo = GetPhotoWithTagByURL(url, lvItem, fileIndex)
                            Dim stream As Stream = New MemoryStream(photo.Data)
                            Package.AddStream(stream, photo.Name)
                        End If
                    Catch ex As Exception
 
                    End Try
                Next
            Else
            End If
 

            SendZipToClient(memStream, Package

 

Private Sub SendZipToClient(memStream As MemoryStream, Package As ZipPackage)
 
        Dim FileName As String = ddVehicleList.SelectedItem.Text.Trim.Replace(" ", "_")
 
        Package.Close(False)
        memStream.Position = 0
 
        If memStream IsNot Nothing AndAlso memStream.Length > 0 Then
            Response.Clear()
            Response.AddHeader("content-disposition", "attachment; filename=" + FileName + "_images.zip")
            Response.ContentType = "application/zip"
            Response.BinaryWrite(memStream.ToArray())
            Response.[End]()
        End If
 
    End Sub

 

 

 This is what I have so far with ZipArchive (gives me an invalid zip file):

Dim VehicleID As Int32 = CInt(ddVehicleList.SelectedValue)
 
            Dim memStream As New MemoryStream()
            Dim Archive As New ZipArchive(memStream, ZipArchiveMode.Create, False, System.Text.Encoding.UTF8)
 
            If VehicleID <> 0 Then
                Dim fileIndex As Int32 = 0
                For Each lvItem As ListViewItem In lvImages.Items
                    Try
                        Dim cbSelected As CheckBox = lvItem.FindControl("cbSelected")
                        If cbSelected.Checked Then
                            fileIndex += 1
                            Dim url As String = cbSelected.Attributes("url")
                            Dim photo As ZipData.Photo = GetPhotoWithTagByURL(url, lvItem, fileIndex)
                            Dim stream As Stream = New MemoryStream(photo.Data)
                            'Package.AddStream(stream, photo.Name)
 
                            Dim lAttachment As ZipArchiveEntry = Archive.CreateEntry(photo.Name)
                            Using lAttachmentStream = lAttachment.Open()
                                Using lStreamWriter = New StreamWriter(stream)
                                    lStreamWriter.Flush()
                                End Using
                            End Using
                            lAttachment.Dispose()
 
 
                        End If
                    Catch ex As Exception
 
                    End Try
                Next
            Else
            End If
 
            SendZipToClient(memStream, Archive)
 
 
Private Sub SendZipToClient(memStream As MemoryStream, Package As ZipArchive)
 
        Dim FileName As String = ddVehicleList.SelectedItem.Text.Trim.Replace(" ", "_")
 
        'Package.Close(False)
        memStream.Position = 0
 
        If memStream IsNot Nothing AndAlso memStream.Length > 0 Then
            Response.Clear()
            Response.AddHeader("content-disposition", "attachment; filename=" + FileName + "_images.zip")
            Response.ContentType = "application/zip"
            Response.BinaryWrite(memStream.ToArray())
            Response.[End]()
        End If
 
    End Sub
 

 Can you please tell me what I am doing wrong?

Thanks.

 Charles

 

 

 

3 Answers, 1 is accepted

Sort by
0
Viktor Tachev
Telerik team
answered on 02 Sep 2015, 01:27 PM
Hi Charles,

Note that you should not close the stream opened by the ZipArchiveEntry.Open() method. Also, make sure that the stream is created like illustrated in the following article.



Regards,
Viktor Tachev
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Charles
Top achievements
Rank 1
answered on 02 Sep 2015, 03:07 PM

Viktor,

 If you look at my code I am not closing the memory stream that I can see. Also, I cannot create the stream as shown in the getting started page as that method creates a file on the server. What I am trying to accomplish is taking a list of image urls and stream them into a zip object then stream that zip to the client all without creating any files on the server. This was all working prior to the redesign (code provided in post). I simply want to get this working again with the latest release. Can you please provide me some code samples that illustrates how to do this? The documentation on this control seems incomplete and offers very little examples on different uses.

 Charles

0
Viktor Tachev
Telerik team
answered on 07 Sep 2015, 09:21 AM
Hello Charles,

Check out the online example below that illustrates a scenario similar to the one you would like to implement.


Note that the third parameter when creating the archive is True. Try to modify your code like illustrated in the demo and see how the behavior changes.


Regards,
Viktor Tachev
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
ZipLibrary
Asked by
Charles
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Charles
Top achievements
Rank 1
Share this question
or