Search code examples
.netsharpziplib

SharpZipLib GetInputStream


I've built a webapplication in which a user can select a zipfile from their filesystem (via an asp:FileUpload). Then the application unzips the zipfile and ftp's every file.

here's the code:

Public Sub Unzip(ByVal str As Stream, ByVal constr As String)
    Dim zf As New ZipFile(str)
    Dim ze As ZipEntry
    Dim i As Integer = 0
    While i < zf.Count
        ze = zf.EntryByIndex(i)
        i = i + 1
        Dim ftp As New ftpItem(constr)
        ftp.upload(ze.Name, "pic", zf.GetInputStream(i), ze.Name, ze.Name)

    End While
    zf.Close() 
End Sub

The ftpItem class is a class of my own which handles the ftp. ftp.upload needs as third parameter the stream for the file to be sent.

But for some reason zf.GetInputStream(i) always gives nothing.


Solution

  • For one thing, you should increment i inside the loop AFTER you call GetInputStream. If there is only one file this logic will always fail, I imagine.

    While i < zf.Count
        ze = zf.EntryByIndex(i)
    
        Dim ftp As New ftpItem(constr)
        ftp.upload(ze.Name, "pic", zf.GetInputStream(i), ze.Name, ze.Name)
    
        i = i + 1
    
    End While
    

    If that does not work, there is a purportedly working C# example here, using a different method GetNextEntry to iterate the list of compressed files.