Zip - AddPackFile - existing Container

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Sebastian0107
New User
New User
Posts: 4
Joined: Fri Mar 18, 2016 3:26 pm

Zip - AddPackFile - existing Container

Post by Sebastian0107 »

It should be possible to add a file to a existing Zip-Container.
collectordave
Addict
Addict
Posts: 1310
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Zip - AddPackFile - existing Container

Post by collectordave »

Thought the same but also thought it should be possible allready with what we have so came up with a bit of code to allow that from a backup project I am doing.

I have split the programme flow into three parts shown by the three buttons.

UnZip,Add and ReZip.

Click UnZip and you are prompted for a zip file. This is first unzipped into a Temp\ folder in the same folder as the zip file. You can then add or delete as many files in this temporary folder as you wish manually using explorer or to add a file click the add button select the file to add and then select where in the zip file you want it placed. The rezip button will then recreate the original zip file from this temporary folder as long as you do not close the programme in between.

Tested on windows 7.0 and Ok. Can obviously be improved so please leave any suggestions or bug reports here.

Please backup your zip files before using as no warrenty.

Code: Select all

UseZipPacker()

Structure Entry
  type.i
  Name.s
EndStructure

Global NewList Content.Entry()

Global Window_0
Global TempFolder.s,ZipFile.s
Global btnUnZip.i,btnAdd.i,btnZip.i,PackID.i

Window_0 = OpenWindow(#PB_Any, 0, 0, 250, 50, "", #PB_Window_SystemMenu)
btnUnZip = ButtonGadget(#PB_Any, 10, 10, 70, 20, "UnZip")
btnAdd = ButtonGadget(#PB_Any, 90, 10, 70, 20, "Add File")
btnZip = ButtonGadget(#PB_Any, 170, 10, 70, 20, "Zip")

Procedure CreatePath(Folder.s)
    
  Define Path.s,Temp.s
  Define BackSlashs.i,iLoop.i
    
  BackSlashs = CountString(Folder, "\")

  For iLoop = 1 To BackSlashs + 1
    Temp = StringField(Folder, iLoop, "\")
    
    If StringField(Folder, iLoop + 1, "\") > ""
      Path + Temp + "\"
    Else
      Path + temp
    EndIf

    CreateDirectory(Path)
    
  Next iLoop
  
EndProcedure

Procedure GetFolderContent(Folder.s,Level.i)
  
  Define ExamDir.i = Level + 1
  Define FName.s
  
    If ExamineDirectory(ExamDir,Folder,"*.*")
    While NextDirectoryEntry(ExamDir)
      type=DirectoryEntryType(ExamDir)
      FName=DirectoryEntryName(ExamDir)
      If type=2 ; Folder.
        If FName<>"." And FName<>".."
          AddElement (Content())
          content()\type = 1
          Content()\Name = Folder + FName + "\"
          GetFolderContent(Folder + FName + "\",ExamDir)
        EndIf
      Else ; File.
          AddElement (Content())
          content()\type = 2
          Content()\Name = Folder + FName
      EndIf
    Wend
    FinishDirectory(ExamDir)
  EndIf
  SortStructuredList(Content(), #PB_Sort_Ascending,OffsetOf(Entry\Name),#PB_String )

EndProcedure

Procedure UnZip()
  
  Define Folder.s,NewFolder.s,Pattern.s
  
  Pattern = "Zip Files (*.zip)|*.zip"
  ZipFile = OpenFileRequester("Please choose ZIP File", "", Pattern,0)
  Folder = "Temp"
  TempFolder = GetPathPart(ZipFile)
  PackID = OpenPack(#PB_Any, ZipFile,#PB_PackerPlugin_Zip)
  If ExaminePack(PackID)
    While NextPackEntry(PackID)
      NewFolder = GetPathPart(PackEntryName(PackID))
      CreatePath(TempFolder + Folder + "\" + NewFolder)
      UncompressPackFile(PackID, TempFolder + Folder + "\" + PackEntryName(PackID)) 
    Wend
    ClosePack(PackID)
    RenameFile(ZipFile, GetPathPart(ZipFile) + "(old)" + GetFilePart(ZipFile))  
  EndIf
  
EndProcedure

Procedure AddFile()
  
  Define FileToAdd.s,WhereToAdd.s,Pattern.s
  
  Pattern = "All Files (*.*)|*.*"
  FileToAdd = OpenFileRequester("Please choose file To Add", "", Pattern,0)
  WhereToAdd = SaveFileRequester("Please Choose Where To Add", "C:\Temp\Code Library\" + GetFilePart(FileToAdd), Pattern,0)
  CopyFile(FileToAdd, WhereToAdd)

EndProcedure

Procedure ReZip(FromFolder.s)
  
  Define FileToPack.s,PackedFile.s
  
  ClearList(Content())
  
  GetFolderContent(FromFolder,0)
  
  ;Add Folders To ZIP
  ForEach Content()
    If content()\type = 1
      PackedFile  = Right(content()\Name,Len(content()\Name) - Len(TempFolder))
      AddPackMemory(PackID,0,0,PackedFile)
    EndIf
  Next
  
  ;Add Files To ZIP
  ForEach Content()
    If content()\type = 2
      PackedFile  = Right(content()\Name,Len(content()\Name) - Len(TempFolder))
      AddPackFile(PackID, content()\Name, PackedFile)
    EndIf
  Next

  EndProcedure

Repeat
  Event = WaitWindowEvent()
  Select event
    Case #PB_Event_CloseWindow
      End

    Case #PB_Event_Gadget
      Select EventGadget()
        Case btnUnZip
          UnZip()
        Case btnAdd
          AddFile()
        Case btnZip
          PackID = CreatePack(#PB_Any, ZipFile) 
          TempFolder = TempFolder + "Temp\"
          ReZip(TempFolder)
          ClosePack(PackID)
          DeleteFile(GetPathPart(ZipFile) + "(old)" + GetFilePart(ZipFile))
          DeleteDirectory(TempFolder,"",#PB_FileSystem_Recursive)
      EndSelect
  EndSelect
ForEver

Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
User avatar
Sebastian0107
New User
New User
Posts: 4
Joined: Fri Mar 18, 2016 3:26 pm

Re: Zip - AddPackFile - existing Container

Post by Sebastian0107 »

Your code works fine, but the mimetype gets the "deflate" state, so it is not possible to get a valide OpenDocument-File... :(
collectordave
Addict
Addict
Posts: 1310
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Zip - AddPackFile - existing Container

Post by collectordave »

I found that after repacking the resultant file is a valid Open Office file. tried modyfing only spreadsheets at the moment. The code for this is here http://www.purebasic.fr/english/viewtop ... 12&t=65415 and works.
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
Post Reply