How can I stop Ionic zip from appending null to the end of a file

93 views Asked by At

I have a VB.net (ASPX) which receives a file (data stream) from my application, and depending on switches will save the file as is or compress & encrypt it using ionic.zip. When a file is saved without encryption it is byte perfect. When the same file is compressed/encrypted and then decrypted/uncompressed there is an extra null (ascii(0)) character appended to the end of the decrypted file. This is generally not an issue, but MS Office products complain about corrupted files (then opens them fine).

The code I use is fairly straightforward and I cannot see any issues. I found nothing in my searches for this issue with ionic zip. Here is the code;

<%@ Page Language="VB" validateRequest="false"%>
<%@ Import Namespace="Ionic.Zip"%>
<%@ Import Namespace="System.Web.HttpContext"%> 

<script Runat="Server">

    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

        Dim strToken As String = Request.QueryString("token")
        Dim strSessionID As String = Request.QueryString("SessionID")
        Dim strAlternateFileName As String = Request.QueryString("AlternateFileName")
        Dim strDestinationFolder As String = Request.QueryString("DestinationFolder")
        Dim strZipYN As String = Request.QueryString("ZipYN")
        Dim strZipFileName As String = Request.QueryString("ZipFileName")
        Dim strZipPassword As String = Request.QueryString("ZipPassword")
        Dim fileName As String = System.IO.Path.GetFileName(Request.Files(0).FileName)
        If IsNothing(Current.Session("token" & strSessionID)) _
        Then
            Current.Session("token") = strToken
            Current.Session("token" & strSessionID) = Current.Session("token")
        End If
        If Len(strAlternateFileName) > 0 _
        Then
            fileName = strAlternateFileName
        End If
        If strZipYN <> "Y" _
        Then
            Request.Files(0).SaveAs(Server.MapPath(strDestinationFolder + "/") + fileName)
        Else
            Dim fileIn As HttpPostedFile
            Dim objZipFile As ZipFile = New ZipFile
            fileIn = Request.Files(0)
            Dim intFileInLen = fileIn.ContentLength
            Dim bytFielIn(intFileInLen) As Byte
            Dim strmFileIn As System.IO.Stream
            strmFileIn = fileIn.InputStream
            strmFileIn.Read(bytFielIn, 0, intFileInLen)
            If strZipPassword.Length > 0 _
            Then
                objZipFile.Password = strZipPassword
                objZipFile.Encryption = EncryptionAlgorithm.WinZipAes256
            End If
            objZipFile.AddEntry(fileName, bytFielIn)
            objZipFile.SaveSelfExtractor(Server.MapPath(strDestinationFolder + "/") + strZipFileName + ".exe", SelfExtractorFlavor.WinFormsApplication)
'            objZipFile.SaveSelfExtractor(Server.MapPath(strDestinationFolder + "/") + strZipFileName + ".zip", SelfExtractorFlavor.WinFormsApplication)
            objZipFile.Dispose()
            objZipFile = Nothing

        'Create a PW protected ZIP only for non windows computers.
        objZipFile = New ZipFile
            If strZipPassword.Length > 0 _
            Then
                objZipFile.Password = strZipPassword
                objZipFile.Encryption = EncryptionAlgorithm.WinZipAes256
            End If
            objZipFile.AddEntry(fileName, bytFielIn)
            objZipFile.Save(Server.MapPath(strDestinationFolder + "/") + strZipFileName + ".zip")
            objZipFile.Dispose()
            objZipFile = Nothing


        End If
            
    
    End Sub
    
</script>

Any ideas?

Edit:

I get the same results whether I extract the file from the ".exe" file, or the ".zip" file

0

There are 0 answers