Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Making the content list of files inside a Zip ?
#8
Very cool @visionmercer

I hope you don't mind but I took the liberty of converting the subroutine to a function . The code also starts populating the array at index position 1 instead of 0.

I made a few minor changes in the function to streamline a bit and to close the file when finished.

By the way, this code only works with .ZIP files and not .7z files.

Code: (Select All)
DIM i AS LONG
DIM filename AS STRING
DIM filenumber AS LONG
REDIM filelist(0) AS STRING

filename = "test3.zip"
filenumber = zipfilearray(filename, filelist())
IF filenumber THEN

    PRINT "Total files found:"; filenumber
    PRINT
    FOR i = 1 TO filenumber
        PRINT CHR$(34); filelist(i); CHR$(34)
    NEXT i
ELSE
    PRINT "No files found or ZIP file does not exist."
END IF

FUNCTION zipfilearray (zipfile AS STRING, strarr() AS STRING)

    TYPE ziphead
        signature AS STRING * 4
        version AS INTEGER
        flags AS INTEGER
        compression AS INTEGER
        modtime AS INTEGER
        moddate AS INTEGER
        crc_32 AS LONG
        compressedSize AS LONG
        uncompressedSize AS LONG
        filenameLen AS INTEGER
        extrafieldLen AS INTEGER
    END TYPE

    DIM head AS ziphead
    DIM ff AS LONG
    DIM fname AS STRING
    DIM Sig AS STRING * 4

    REDIM strarr(0) AS STRING
    IF _FILEEXISTS(zipfile) THEN
        Sig = "PK" + CHR$(&H03) + CHR$(&H04) ' note: only works with .ZIP files
        ff = FREEFILE
        OPEN zipfile FOR BINARY AS ff
        DO
            GET ff, , head
            IF head.signature = Sig THEN
                fname = SPACE$(head.filenameLen)
                GET ff, , fname
                REDIM _PRESERVE strarr(UBOUND(strarr) + 1) AS STRING
                strarr(UBOUND(strarr)) = fname
                SEEK ff, SEEK(ff) + head.extrafieldLen + head.compressedSize
            END IF
        LOOP UNTIL EOF(ff) OR head.signature <> Sig
        CLOSE ff
    END IF
    zipfilearray = UBOUND(strarr)

END FUNCTION
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Reply


Messages In This Thread
RE: Making the content list of files inside a Zip ? - by TerryRitchie - 02-06-2024, 03:20 PM



Users browsing this thread: 3 Guest(s)