[Implemented] Packer -add unpack to disk function

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

[Implemented] Packer -add unpack to disk function

Post by IdeasVacuum »

Currently, there is no stored record of the file names packed and no function to unpack them to disk. Yet that cannot be an unusual requirement?

So, this: UnpackToDisk(TargetFolderPath, PackFile)
Would be better than this:

Code: Select all

Procedure PackIt(sPackFolder.s,sPackFile.s)
;-----------------------------------------
Protected NewList DataFiles.s()
Protected sListOfFiles.s = sPackFolder + "ListOfFiles.txt"
Protected          iHd.i = 0
Protected iListOfFiles.i = 1

        If(CreateFile(iListOfFiles,sListOfFiles))

                   iHd = ExamineDirectory(#PB_Any, sPackFolder, "*.*")
                If iHd

                       While NextDirectoryEntry(iHd)

                             If DirectoryEntryType(iHd) = #PB_DirectoryEntry_File

                                   If Not (DirectoryEntryName(iHd) = "Thumbs.db")

                                                        AddElement(DataFiles())
                                                                   DataFiles() = sPackFolder + DirectoryEntryName(iHd)
                                          WriteString(iListOfFiles,DirectoryEntryName(iHd) + #LF$,#PB_Ascii)
                                   EndIf
                             EndIf
                       Wend

                       FinishDirectory(iHd)
                             CloseFile(iListOfFiles)

                          DeleteFile(sPackFile) ;delete existing file, if it exists

                       If CreatePack(sPackFile)

                               AddPackFile(sListOfFiles,2)

                               ForEach DataFiles()

                                       AddPackFile(DataFiles(),2)
                               Next

                                     ClosePack()
                               DeleteDirectory(sPackFolder,"*.*",#PB_FileSystem_Force)
                                     ClearList(DataFiles())
                       EndIf
                EndIf
        EndIf

EndProcedure

Procedure UnPackIt(sPackFolder.s,sPackFile.s)
;--------------------------------------------
Protected NewList FullPath.s()
Protected     sFileName.s = ""
Protected sFileNameList.s = "" ;huge string
Protected         iSize.i = 0
Protected      iFileCnt.i = 0
Protected   iTotalFiles.i = 0
Protected      iFileOut.i = 1

       If(OpenPack(sPackFile))

               CreateDirectory(sPackFolder)

               ;First file in pack is a list of filenames
                  *ListOfFiles = NextPackFile()
                         iSize = PackFileSize()

               If(*ListOfFiles And iSize)

                        sFileNameList = PeekS(*ListOfFiles,iSize,#PB_Ascii)
                          iTotalFiles = CountString(sFileNameList,#LF$)

                      For iFileCnt = 1 To iTotalFiles

                               sFileName = StringField(sFileNameList,iFileCnt,#LF$)

                              AddElement(FullPath())
                                         FullPath() = sPackFolder + sFileName
Debug FullPath()
                      Next

                      ForEach FullPath()

                               *File = NextPackFile()
                               iSize = PackFileSize()
Debug iSize
                             If(*File And iSize)

                                    If CreateFile(iFileOut,FullPath())

                                            WriteData(iFileOut,*File,iSize)
                                            CloseFile(iFileOut)
                                    EndIf
                             EndIf
                      Next
               EndIf

               ClosePack()
               ClearList(FullPath())
       EndIf

EndProcedure
Edit: Changed the code above to be more generic, should anyone wish to make use of it. However, there is a much more comprehensive solution posted by Inf0Byt3: http://www.purebasic.fr/english/viewtopic.php?t=30353
IdeasVacuum
If it sounds simple, you have not grasped the complexity.