Home Forums .NET libraries Xceed Zip & Real-Time Zip for .NET TestZipFile Equivalent

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

    We are just upgrading an application from VB6 to VB.NET. The old application was using Xceen Zip Library 4.5, and we are going to Xceed Zip for .NET 2.0. One of the methods that we use in 4.5 is called “TesdtZipFile”, I cannot seem to find an equivalent methid in the .NET 2.0 Library.

    Is there a version of TestZipFile in the Xceed Zip for .NET 2.0 Library, and what is it called.

    Thanks

    Imported from legacy forums. Posted by dmrcgyap (had 8557 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi!

    There is no built-in equivalent, but I already did a sample for another client, so here an implementation of TestZipFile:

    <code> private static void TestZipFile( AbstractFile zipFile )
    {
    try
    {
    ZipEvents events = new ZipEvents();
    events.ItemException += new ItemExceptionEventHandler( events_ItemException );

    // This can throw if the central header does not work.
    ZipArchive archive = new ZipArchive( events, null, zipFile );

    foreach( AbstractFile file in archive.GetFiles( events, null, true ) )
    {
    using( Stream readStream = file.OpenRead( events, null ) )
    {
    byte[] buffer = new byte[ 32768 ];

    while( readStream.Read( buffer, 0, buffer.Length ) > 0 )
    ;
    }
    }
    }
    catch( Exception except )
    {
    throw new Exception( “Testing this zip file failed.”, except );
    }
    }

    private static void events_ItemException(object sender, ItemExceptionEventArgs e)
    {
    Console.WriteLine( “Error reading zip file: {0}”, e.Exception.Message );
    Console.WriteLine( “Ignoring error…” );

    e.Action = ItemExceptionAction.Ignore;
    }
    </code>

    Imported from legacy forums. Posted by Martin (had 344 views)

    User (Old forums)
    Member
    Post count: 23064

    I am having a problem implementing the test code you sent me. If I try to open a file that is not really a ZIP file (that is what we are trying to do here, determine if a file is a ZIP file or not).

    Anyway, when I open a non ZIP file I get an exception (as expected) but later I get another exception that I cannot catch when the GC is trying to do some cleanup and is calling a Finalizer on an Xceed.Zip.ZippedFolder object. The exception is an ArgumentNullException. I have a screen capture of the error in Visual studio, how can I post a graphic to you?

    Thanks

    Anthony

    Imported from legacy forums. Posted by dmrcgyap (had 458 views)

    User (Old forums)
    Member
    Post count: 23064

    Send an email to <mail>support@xceedsoft.com</mail>

    Imported from legacy forums. Posted by Martin (had 322 views)

    User (Old forums)
    Member
    Post count: 23064

    Can you share the solution to detect if the file is a zip file or not? I saw this topic went offline and would love to see the final function.

    Thanks

    Imported from legacy forums. Posted by wcombs (had 648 views)

    User (Old forums)
    Member
    Post count: 23064

    There are a couple of points to solving our problem. First I needed the latest patch level on Xceed Zip to solve the NullReference Exception. Next I found that Xceed is very slow at catching a bad IP file a nd throwing an exception so I wrote a small function to peek at the first two bytes of the file and check for “PK” if this is missing, I assume the file is not a ZIP file and this spped up the process most of the time.

    Anyway, Here is the completed functions

    ——————————————————-
    Private Function CheckZip() As Boolean
    Dim blnResult As Boolean = True
    Dim objItemList() As Xceed.Zip.QuickZipItem = Nothing

    If CheckZipHeader() = False Then Return False

    Try
    Xceed.Zip.Licenser.LicenseKey = “*******************”
    objItemList = Xceed.Zip.QuickZip.GetZipContents(m_SysFileName, “*”)
    Catch ex As Xceed.Zip.QuickZipException
    blnResult = False
    Finally
    objItemList = Nothing
    End Try

    Return blnResult
    End Function

    Private Function CheckZipHeader() As Boolean
    Dim blnResult As Boolean = False
    Dim objStream As Stream = Nothing
    Dim bytHdr(1) As Byte
    Dim strHdr As String = String.Empty

    Try
    objStream = File.OpenRead(m_SysFileName)
    bytHdr(0) = objStream.ReadByte()
    bytHdr(1) = objStream.ReadByte()
    strHdr = Encoding.ASCII.GetString(bytHdr)
    If strHdr = “PK” Then blnResult = True
    Catch ex As Exception
    blnResult = False
    Finally
    If Not (objStream Is Nothing) Then
    objStream.Close()
    End If
    End Try

    Return blnResult
    End Function

    Imported from legacy forums. Posted by dmrcgyap (had 436 views)

    User (Old forums)
    Member
    Post count: 23064

    Be carefull! Self-extracting zip files do not start with “PK”, but with the good old “MZ” EXE signature. And though spanned zip files do start with “PK”, some apps, when writing on removable media, start by writing the “PK” signature, then replace it with zeros or other letters if the zip file finally did not span. The real PK is then at offset 4, not 0.

    The real way to detect a zip file is to look backward from the end of the file for the 0x50, 0x4b, 0x05, 0x06 ending header signature, but no more than in the last 64kb.

    If you find this signature, then it may be a zip file. You could then double-check to see if the ending header’s central directory offset points to the 0x50, 0x4b, 0x07, 0x08 central header signature. If so, it’s most probably a zip file.

    I’ll try to address this in my blog. It’s a good topic.

    Imported from legacy forums. Posted by Martin (had 316 views)

    User (Old forums)
    Member
    Post count: 23064

    Would this functionality be considered a verify? I need to verify that the zip operation has worked, so given a set of files, would this functionality be considered the same?

    I’m actually having to implement a set of backup routines using Zip libraries, because the restore portion of the backup component worked in a very differnt way to the way I expected it to.

    If it is, is it possible to post a bit of source code for the complete solution, preferably in C#, otherwise any other language, and I will convert.

    Cheers,

    MP.

    Imported from legacy forums. Posted by MartinPlatt (had 416 views)

    User (Old forums)
    Member
    Post count: 23064

    Yes, we could say that this is a traditional “Verify a zip file” routine. Though uncompressed data isn’t compared with the original, the CRC32 is verified, thus any difference less than 11 bits (not sure or that number, any CRC32 wizz out there?) would be detected by the CRC verification at the end of decompression.

    Imported from legacy forums. Posted by Martin (had 401 views)

    User (Old forums)
    Member
    Post count: 23064

    Hello!

    Your example is most helpful, however, I copied it to use in testing our zips, and I’ve been stepping through it in debug mode, and the events_ItemException never gets called from the new ZipArchive, instead it goes directly to the catch and displays the

    “A 0-length filename was found in a central header of the zip file.”

    exact message I’m trying to catch and bypass. Argggg.

    If I’m not mistaken, the code you laid out, should go to events_ItemException when there is a exception in creating the ZipArchive? I’m using the newest version 2.2.5302.0. And, I would ask if I could be doing anything wrong, but I’ve copied your example verbatim.

    Any suggestions?

    Patrick
    ps: I’ve used the forums quite a bit to find ideas, and this is my first entry… so, I’ll take this chance to say that I love the products, and love the forum. Thanks!!!

    Imported from legacy forums. Posted by PCooper (had 493 views)

    User (Old forums)
    Member
    Post count: 23064

    Not all errors end-up causing an ItemException event. Some are considered fatal errors. But in this case, I agree it should at least have triggered the event. Worse, it probably should not cause an error in the first place. With some applications, data from the console’s standard output can be compressed in a zip file, causing an empty filename.

    You should send an email to support@xceedsoft.com, and I’ll take a look at this.

    Imported from legacy forums. Posted by Martin (had 7489 views)

    User (Old forums)
    Member
    Post count: 23064

    How to write this code in Vb.net.

    I am tryiny to test the zip file.  If the zip file corrupt, I do not want it throw exception error, , so my program can keep going.

    Imported from legacy forums. Posted by allen (had 2071 views)

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