Parse zip file without using a library

Share your advanced PureBasic knowledge/code with the community.
Little John
Addict
Addict
Posts: 4791
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Post by Little John »

PB wrote:The Goto thing was a joke. :) But since there was a reaction, how could the
code be modified NOT to use it, since so many people say that any code can
be written without Goto if they really wanted to. To those people: show me.
Just look at the original VB code. There is no Goto at all. ;-)

Regards, Little John
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Post by PB »

Can this be modified to parse a cab file? I heard cabs are just zips?
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
gnozal
PureBasic Expert
PureBasic Expert
Posts: 4229
Joined: Sat Apr 26, 2003 8:27 am
Location: Strasbourg / France
Contact:

Post by gnozal »

PB wrote:I heard cabs are just zips?
Imho M$ cabinet files (CAB) are not just zip files, neither are Installshield archives (also CAB).
For free libraries and tools, visit my web site (also home of jaPBe V3 and PureFORM).
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re:

Post by Dude »

Does someone know how to convert srod's example to Unicode? It currently lists all zip names in Asian characters on my English PC.
User avatar
JHPJHP
Addict
Addict
Posts: 2258
Joined: Sat Oct 09, 2010 3:47 am

Re: Parse zip file without using a library

Post by JHPJHP »

Hi Dude,

It works if the Unicode switch is unchecked from the Menu -- Compiler Options, or...

Here is a version that works with or without the Unicode option checked:

Code: Select all

Structure typZipLocalFileHead
  zlfhSignature.l
  zlfhVersion.w
  zlfhBitFlag.w
  zlfhCompression.w
  zlfhModFileTime.w
  zlfhModFileData.w
  zlfhCRC.l
  zlfhCompressedSize.l
  zlfhUncompressedSize.l
  zlfhFileNameLength.w
  zlfhExtraFieldLength.w
EndStructure

Procedure ListZipFile(Filename.s)
  ReadHead.typZipLocalFileHead
  FF = ReadFile(#PB_Any, Filename)

  If FF
    Repeat
      ReadHead\zlfhSignature = ReadLong(ff)

      If ReadHead\zlfhSignature = $4034B50
        FileSeek(FF, Loc(ff) - 4)
        ReadData(FF, @ReadHead, SizeOf(ReadHead))
  
        With ReadHead
          If \zlfhFileNameLength <> 0
            *FileString = AllocateMemory(\zlfhFileNameLength)
            ReadData(FF, *FileString, \zlfhFileNameLength)
            Debug PeekS(*Filestring, \zlfhFileNameLength, #PB_Ascii)
            FreeMemory(*FileString)
          EndIf
          Debug"  Compressed = "  + Str(\zlfhCompressedSize) + " UnCompressed = "  + Str(\zlfhUnCompressedSize)
          SeekSize = \zlfhCompressedSize

          If \zlfhExtraFieldLength : SeekSize + \zlfhExtraFieldLength : EndIf
          If (\zlfhBitFlag & $4) : SeekSize + 12 : EndIf

        EndWith
        Seeksize + Loc(FF)
        FileSeek(FF, Seeksize)
        Filecount + 1
      Else
        If fileCount = 0 : Debug "No valid ZIP" : EndIf

        quitLoop = 1
      EndIf
    Until quitLoop
    Debug Str(Filecount) + " in ZipFile"
    CloseFile(FF)
  EndIf
EndProcedure

FileName.s = OpenFileRequester("Please choose a ZIP file to load", "", "ZIP File (*.zip)|*.zip|All files (*.*)|*.*", 0)

If SizeOf(FileName) > 0 : ListZipFile(FileName) : EndIf

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Parse zip file without using a library

Post by Dude »

Thanks JHPJHP, I needed this because my app is Unicode. :)
User avatar
holzhacker
Enthusiast
Enthusiast
Posts: 125
Joined: Mon Mar 08, 2010 9:14 pm
Location: "Mens sana in corpore sano"
Contact:

Re: Parse zip file without using a library

Post by holzhacker »

Dear,

In "Linux Ubuntu 14.04 LTS - Gnome 3.0" does not recover the file names, other information this recovering Ok.

In time, clearing the unicode compiler option it works as expected on Linux.
Greetings and thanks!

Romerio Medeiros
romerio@gmail.com
Post Reply