Home Forums .NET libraries Xceed Zip & Real-Time Zip for .NET Test existing archive

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #20042 |

    I hate to post this because I feel it must have been asked many times before. I have searched the forum, reviewed the documentation but I just don’t see it.

     How do you test the validity of a zip archive?

    I use another 3rd party library that simply has a testArchive() method that returns a boolean value. True if the zip is good and false if it is corrupt in any way. Since I am already using Xceed FTP to retrieve the zip file from a remote site, I would like to use either C# or Xceed to test the zip.

    Imported from legacy forums. Posted by Chris (had 1722 views)

    User (Old forums)
    Member
    Post count: 23064

    The component does not offer this feature.  However, it can be easily implemented by simply getting an instance of each file in the zip archive, open a stream on each, and read it through.  This will first make sure that the archive itself is valid (central directory), and each file is valid (the CRC will be checked) and can be unzipped.

    e.g.:

    private void ValidateArchive( string archiveToTest )

    {

        DiskFile zip = new DiskFile( archiveToTest );

        if( zip.Exists )

        {

            try

            {

                ZipArchive archive = new ZipArchive( zip );

                byte[] buffer = new byte[ 32768 ];

                int bytesRead = 0;

                foreach( ZippedFile zippedFile in archive.GetFiles( true ) )

                {

                    using( Stream sourceFile = zippedFile.OpenRead() )

                    {

                        while( ( bytesRead = sourceFile.Read( buffer, 0, buffer.Length ) ) > 0 )

                        {

                        }

                    }

                }

            }

            catch( Exception ex )

            {

                //place a true exception handling routine here; if an exception is thrown,

                //this means there is a problem with the archive

                Debug.WriteLine( ex.ToString() );

            }

        }

    }

     

    Imported from legacy forums. Posted by André (had 525 views)

    User (Old forums)
    Member
    Post count: 23064

    Someone asked me for the VB.NET version, so here it is :

    Private Sub ValidateArchive( ByVal archiveToTest As String )

        Dim zip As New DiskFile( archiveToTest )

        If zip.Exists Then

            Try

                Dim archive As New ZipArchive( zip )

                Dim buffer( 32767 ) As Byte

                Dim bytesRead As Integer = 0

                For Each zippedFile As ZippedFile In archive.GetFiles( True )

                    Using sourceFile As Stream = zippedFile.OpenRead()

                        Do

                            bytesRead = sourceFile.Read( buffer, 0, buffer.Length )

                        Loop While bytesRead > 0

                    End Using

                Next zippedFile

            Catch ex As Exception

                ‘place a true exception handling routine here; if an exception is thrown,

                ‘this means there is a problem with the archive

                Debug.WriteLine( ex.ToString() )

            End Try

        End If

    End Sub

    Imported from legacy forums. Posted by André (had 1320 views)

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.