With CreatePack() and zip extension it does not give a program error and all directories (even empty ones) and all files are merged.
With 7z and tar extension the program gives an error on the same line (number 45).
[ERROR] Invalid memory access. (write error at address 0)
Code: Select all
EnableExplicit
;Initialize Packer Plugin
UseZipPacker() ;#PB_PackerPlugin_Zip
UseLZMAPacker() ;#PB_PackerPlugin_Lzma
UseTARPacker() ;#PB_PackerPlugin_Tar
Structure PackData
Count.i
Current.i
BaseDir.s
EndStructure
;Pass 1: Count total files and directories for the progress bar.
Procedure CountItems(Path.s, *Data.PackData)
Protected Dir = ExamineDirectory(#PB_Any, Path, "*.*")
If Dir
While NextDirectoryEntry(Dir)
If DirectoryEntryName(Dir) <> "." And DirectoryEntryName(Dir) <> ".."
*Data\Count + 1
If DirectoryEntryType(Dir) = #PB_DirectoryEntry_Directory
CountItems(Path + DirectoryEntryName(Dir) + #PS$, *Data)
EndIf
EndIf
Wend
FinishDirectory(Dir)
EndIf
EndProcedure
;Pass 2: Add items to the pack and update Progress Bar.
Procedure AddToPack(Path.s, PackID, *Data.PackData)
Protected Dir = ExamineDirectory(#PB_Any, Path, "*.*")
Protected Name.s, RelativePath.s
If Dir
While NextDirectoryEntry(Dir)
Name = DirectoryEntryName(Dir)
If Name <> "." And Name <> ".."
; Calculate relative path for the archive structure
RelativePath = ReplaceString(Path + Name, *Data\BaseDir, "")
If DirectoryEntryType(Dir) = #PB_DirectoryEntry_Directory
;Add empty or full directory.
AddPackDirectory(PackID, RelativePath + #PS$) ;Give ERRor by Create "7z" and "tar" Fie.
AddToPack(Path + Name + #PS$, PackID, *Data)
Else
;Add file (works even if empty).
AddPackFile(PackID, Path + Name, RelativePath)
EndIf
;Update Progress.
*Data\Current + 1
SetGadgetState(0, *Data\Current)
;Refresh UI to keep it responsive.
While WindowEvent(): Wend
EndIf
Wend
FinishDirectory(Dir)
EndIf
EndProcedure
; --- Main GUI ---
Define SourceDir.s = PathRequester("Select folder to pack", "")
If SourceDir = "": End: EndIf
Define DestFile.s = SaveFileRequester("Save Pack As", "archive.zip", "Zip (*.zip)|*.zip", 0)
;Define DestFile.s = SaveFileRequester("Save Pack As", "archive.7z", "7z (*.7z)|*.7z", 0)
;Define DestFile.s = SaveFileRequester("Save Pack As", "archive.tar", "Tar (*.Tar)|*.tar", 0)
If DestFile = "": End: EndIf
OpenWindow(0, 0, 0, 400, 80, "Packing...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ProgressBarGadget(0, 10, 30, 380, 20, 0, 100)
Define Info.PackData
Info\BaseDir = SourceDir
;Step 1: Count.
CountItems(SourceDir, @Info)
SetGadgetAttribute(0, #PB_ProgressBar_Maximum, Info\Count)
;Step 2: Create and Fill Pack.
Define Pack = CreatePack(#PB_Any, DestFile, #PB_PackerPlugin_Zip) ;all Directorys (43) and all Files (110).
;Define Pack = CreatePack(#PB_Any, DestFile, #PB_PackerPlugin_Lzma) ;[ERROR] Invalid memory access. (write error at address 0) (line 45).
;Define Pack = CreatePack(#PB_Any, DestFile, #PB_PackerPlugin_Tar) ;[ERROR] Invalid memory access. (write error at address 0) (line 45).
If Pack
AddToPack(SourceDir, Pack, @Info)
ClosePack(Pack)
MessageRequester("Done", "Archive created successfully!")
Else
MessageRequester("Error", "Could not create pack file.")
EndIf

