EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Secure Black Box Zip corrupt PDF files

Also by EldoS: Rethync
The cross-platform framework that simplifies synchronizing data between mobile and desktop applications and servers and cloud storages
Posted: 02/19/2015 03:55:28
by Mike Ardron (Priority Standard support level)
Joined: 11/03/2011
Posts: 8

We have a license to use SecureBlackbox PGP and ZIP. We have a problem with ZIP, using version

We have written an application (Windows service) to zip up files and upload to an FTP server for customers. This service was launched a couple of weeks ago, and out of 34 documents processed on our test account (in 5 different zip files), 4 PDF documents were corrupted in the process. The 4 documents were from different sources. We get one of the following 3 error messages from each of the documents when opening it from the zip file afterwards:

1 An error exists on this page. Acrobat may not display the page correctly. Please contact the person who created the PDF document to correct the problem.
2 There was an error opening this document. The root object is missing or invalid.
3 There was an error opening this document. There was a problem reading this document (14).

If I run the following basic code, create a zip file in a fixed location on my machine and process each document individually, it works:

Dim outputFile As New Zipfile
Dim outputFilePath As String = "<FilePath><Filename>.zip"


'Get the source doc            
Dim filePath As String = "<FilePath><FileName>.pdf"

'Put the doc into the zip file
Dim memstream As New System.IO.MemoryStream

Dim br1 As BinaryReader = New BinaryReader(System.IO.File.OpenRead(filePath))
Dim buffer1 As Byte() = br1.ReadBytes(br1.BaseStream.Length)
memstream.Write(buffer1, 0, buffer1.Length)
outputFile.AddFileToZip("test.pdf", memstream)


'Now save the zip

The CreateZip function is below:

Private zipFile As New SBArcZip.TElZipWriter

Public Function CreateZip(ByVal Filename As String) As Boolean
                Dim ok As Boolean = True

                zipFile.Encrypt = False

                _zipState = ZipState.Empty
                ZipFileName = Filename            

                Return ok

End Function

The AddFileToZip function is below:

Public Function AddFileToZip(ByVal FileName As String, ByRef memStream As MemoryStream) As Boolean
                Dim ok As Boolean = True
                memStream.Seek(0, SeekOrigin.Begin)
                zipFile.Add(memStream, FileName)


    _zipState = ZipState.FilesAdded                                            
                Return ok
End Function

In the live version of the service, we get a list of documents from the database, add the documents to a zip file, rename them according to their invoice numbers, then add a csv file in at the end with the list of files with their original filenames. Sample code below:

Dim outputFile As New Zipfile

Dim outputFileLocation As String = Settings.OutputFileLocation
Dim outputFilePath As String = "<outputFileLocation><filename>.zip"

Dim dtDocs As DataTable = <database connection>

If dtDocs.Rows.Count > 0 Then
                'Get the invoice documents and place them in the zip file
                Dim memstream As New System.IO.MemoryStream
                Dim documentLocation As String = <DocumentLocation>
                For Each rowDoc As DataRow In dtDocs.Rows
                                Dim originalFilename As String = rowDoc("OriginalDocumentName")
                                Dim docID As String = rowDoc("DocID")
                                Dim docName As String = <DocumentID><FileType>
                                'Location within the directory of the document
                                Dim documentDir As String = <FilePath>
                                Dim br1 As BinaryReader = New BinaryReader(System.IO.File.OpenRead(documentDir & docName))
                                Dim buffer1 As Byte() = br1.ReadBytes(br1.BaseStream.Length)
                                memstream.Write(buffer1, 0, buffer1.Length)
                                outputFile.AddFileToZip(rowDoc("DocID") & "." & rowDoc("FileType"), memstream)
                'Add the csv file
                Dim csvStream As New MemoryStream
                Using writer As New StreamWriter(csvStream)  

                                CreateCSV.WriteDataTable(dtDocs, writer, True)
                                outputFile.AddFileToZip("<docname>.csv", csvStream)


                End Using
End If

CreateZip and AddFileToZip functions as above, SaveZip as below:

Public Function SaveZip() As Boolean
                Dim ok As Boolean = True
    _zipState = ZipState.Written

    Return ok

End Function

Any ideas?

Posted: 02/19/2015 04:12:43
by Ken Ivanov (EldoS Corp.)

Hi Mike,

First of all, please get rid of the BinaryReader class in your code. This class is known to be not binary-friendly enough and may corrupt the source data. I suggest that you open the file as a FileStream and then copy it to a memory stream with SBStreams.Unit.CopyStream() method.

If it doesn't help, please upgrade to the latest SecureBlackbox version (12 at the moment) and check if it exposes the same issue.


Posted: 02/19/2015 08:23:51
by Mike Ardron (Priority Standard support level)
Joined: 11/03/2011
Posts: 8

Unfortunately the issue still occurs.
The source documents can be opened in Adobe Reader with no problem, but the resulting zipped docs are still problematic, even with the switching from Binary Reader to Filestream and using the updated dll files.
Are there any other things we can try?
Posted: 02/19/2015 09:03:52
by Ken Ivanov (EldoS Corp.)


Thank you for checking. I believe we will need to conduct some further research locally.

It would be great if you could capture a sample set of files (before they are added to the ZIP file) which we could use to reproduce the issue in our environment. You can send the files privately to us by posting them to the help desk.

Posted: 02/19/2015 10:01:41
by Mike Ardron (Priority Standard support level)
Joined: 11/03/2011
Posts: 8

Hi Ken

The issue seems to be fixed if a new memory stream is used for writing each file.

Posted: 02/19/2015 10:08:43
by Eugene Mayevski (EldoS Corp.)

Thank you for letting us know. Can you please elaborate (for other future readers of this forum) which part of your code (the one you posted above) was changed and how the new code piece looks like?

On a side note please use CODE button to mark the beginning and the end of the code block in your messages.

Sincerely yours
Eugene Mayevski
Also by EldoS: CallbackProcess
A component to control process creation and termination in Windows and .NET applications.



Topic viewed 643 times

Number of guests: 1, registered members: 0, in total hidden: 0


Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!