Page 1 of 1

Error with Createpack(), 7z and tar.

Posted: Mon Feb 02, 2026 12:49 pm
by Little_man
A program (PB 6.30) to backup directories (even empty ones) with all their files

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
Little_man

Re: Error with Createpack(), 7z and tar.

Posted: Mon Feb 02, 2026 9:39 pm
by infratec
A tar file is a stream archive.
You can not add directoris in this way.
You can simply comment it out:

Code: Select all

;AddPackDirectory(PackID, RelativePath + #PS$)
But then you have no empty directories in the tar file.

This is a kind of a problem:

https://github.com/adamhathcock/sharpco ... issues/808
https://stackoverflow.com/questions/128 ... to-tarfile

So Fred have to look at this.

Re: Error with Createpack(), 7z and tar.

Posted: Tue Feb 03, 2026 8:47 am
by Little_man
Thanks "infratec" for this info

Little_man