Please comment my Unzipper

Everything else that doesn't fall into one of the other PB categories.
User avatar
bluefish
New User
New User
Posts: 4
Joined: Thu Apr 10, 2025 1:47 pm

Please comment my Unzipper

Post by bluefish »

Hello,

I needed a program that unzip a zip file and preserves its directory structure using only Purebasic. I couldn't find one, so I made one :idea:

I tested it against 7zip and the zip extractor built in Windows Explorer. It extracts exactly the same (verified with the SHA-256 hash of the whole extracted directory using 7zip).

Please comment :oops:

Code: Select all

UseZipPacker()

; If CreatePack(0, "monarchive.zip")
;   AddPackFile(0, "monfich.txt", "mondoss/monfich.txt")
;   AddPackFile(0, "monfich.txt", "monfich.txt")
;   ClosePack(0)
; Else
;   Debug "Error creating pack monarchive.zip"
; EndIf

; st = ElapsedMilliseconds()

If OpenPack(1, "secretarchive.zip")
  If ExaminePack(1)
    CreateDirectory("secretarchive")
    SetCurrentDirectory("secretarchive")
    BaseDir$ = GetCurrentDirectory()
    While NextPackEntry(1)
      Debug "Name : " + PackEntryName(1) + " | Type : " + PackEntryType(1) + " | Size : " + PackEntrySize(1)
      nbstr = CountString(PackEntryName(1), "/")
      If nbstr >= 1
        
        Debug "nbstr = " + nbstr
        For i = 1 To nbstr ; création des dossiers (nombre de "/" == nombre de dossiers à créer)
          dir$ = StringField(PackEntryName(1), i, "/")
          resultCreateDir = CreateDirectory(dir$)
          Debug "resultCreateDir "+dir$+" = " + resultCreateDir
          SetCurrentDirectory(GetCurrentDirectory() + dir$)
        Next
        If Right(PackEntryName(1), 1) <> "/" And PackEntryType(1) = #PB_Packer_Directory ; cas où le dernier élément ne termine PAS par "/" mais est un dossier quand même
          dir$ = StringField(PackEntryName(1), nbstr+1, "/")
          resultCreateDir = CreateDirectory(dir$)
          Debug "resultCreateDir LASTDIR "+dir$+" = " + resultCreateDir
        Else
          filename$  = StringField(PackEntryName(1), nbstr+1, "/") ; vérification que le dernier élément est bien un nom de fichier
          If filename$ <> ""                                       ; exemple : si on demande le dernier champ et qu'il est vide alors on un problème
            UncompressPackFile(1, filename$)
          Else
            Debug "PackEntryName invalid : filename$ vide"
          EndIf
        EndIf
        SetCurrentDirectory(BaseDir$)
        
      ElseIf nb = 0
        
        If PackEntryType(1) = #PB_Packer_Directory
          resultCreateDir = CreateDirectory(PackEntryName(1))
          Debug "resultCreateDir NOSLASH "+PackEntryName(1)+" = " + resultCreateDir
        Else
          UncompressPackFile(1, PackEntryName(1))
        EndIf
        
      Else
        
        Debug "INVALID PackName nbstr = " + nbstr
        
      EndIf
    Wend
  Else
    Debug "Can't examine pack"
  EndIf
Else
  Debug "Can't open monarchive.zip"
EndIf

; et = ElapsedMilliseconds()
; 
; Define out.s = Str(et-st) + "ms"
; SetClipboardText(out)

; IDE Options = PureBasic 6.20 (Windows - x64)
; CursorPosition = 65
; FirstLine = 17
; EnableXP
; DPIAware
; Executable = ziptest.exe
; CompileSourceDirectory
User avatar
jacdelad
Addict
Addict
Posts: 1992
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Please comment my Unzipper

Post by jacdelad »

ClosePack()...Just in case your program becomes bigger.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Post Reply