0

我有一个 VB.net (ASPX),它从我的应用程序接收文件(数据流),并根据开关将文件按原样保存或使用 ionic.zip 压缩和加密它。当文件未经加密保存时,它是字节完美的。当同一个文件被压缩/加密然后解密/解压缩时,解密文件的末尾会附加一个额外的空 (ascii(0)) 字符。这通常不是问题,但 MS Office 产品会抱怨文件损坏(然后可以正常打开)。

我使用的代码相当简单,我看不到任何问题。我在使用 ionic zip 搜索此问题时一无所获。这是代码;

<%@ 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>

有任何想法吗?

编辑:

无论是从“.exe”文件还是“.zip”文件中提取文件,我都会得到相同的结果

4

0 回答 0