Packing Files

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by blueb.

I have a simple mind, so I like simple procedure calls... :)

So I decided to write a few simple procedures that would suit my needs, but no matter what I try, I end up creating the packed file in my root directory. As you can see, I change the directory to the desired location, but it seems that CreatePack() overwrites my wishes!

Any ideas? :cry:

--blueb

Code: Select all

; ==================================================================================
; Create 3 simplified Pack Procedures for PureBasic
;
;PackDir("C:\BigJob")  - creates a packed file called: BigJob.pak **in this directory ** 
;PackFile("Fred.doc") - creates a packed file called: fred.pak in same directory
;UnPack("BigJob.pak") - unzips all files in the same directory as file was located.
; ==================================================================================

Procedure.l PackDir(SourceDir$)
; -------------------------------------------------------------------------------------
;#1  PackDir
;PackDir("C:\BigJob")  - creates a packed file called: BigJob.pak **in this directory **
; -------------------------------------------------------------------------------------

;Change to Source Directory
a$ = SourceDir$
If SetCurrentDirectory_(@a$) = 0
  MessageRequester("Error","Can't find directory " + Chr(10) + Chr(13) + "Or doesn't exist! " + Chr(10) + Chr(13) + "Please re-type your choice",0)
  End
EndIf  

If GetCurrentDirectory_( a$, @a$)
  MessageRequester("This is your Current Directory",a$,0)
EndIf

a$ = a$ + ".pak"
hPack.l = CreatePack(a$)
MessageRequester("Zero  = NOT successful", Str(hPack), 0)

Result = ClosePack()

EndProcedure  ; PackDir procedure


; ---------------------------------
; Main

PackDir("c:\BigJob")

BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Paul.

You could try giving your PAK file a name?

Change:
a$ = a$ + ".pak"

to:
a$ = a$ + "\file.pak"


Your current logic will make a$="C:\BigJob.pak" so it will always create the pack file in C: (root)

----------
Visit the PB Resources Site at http://www.reelmediaproductions.com/pb
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by blueb.

Your right Paul, I've been trying to parse the string
name to no avail. :)
but I'm trying to set it up so
it will work as easily with deeply nested directories.
e.g. c:\Jobs\Sept\Week1\BigJob (my current directory)

I want the file to pack all the files in this
directory into BigJob.pak, then erase all the files except
BigJob.pak

If I hard code the name: e.g. BigJob.pak into the procedure
I lose the ability to use it as a generic procedure.

The only way I can see to accomplish this is to parse the
complete pathname for the name "BigJob"
something like:
1 - Take the full pathname
2 - starting at the right side find first "\"
3 - strip off everything up to the this "\" location
4 - Only "BigJob" should remain
5 - Make my packed filename: "BigJob" + ".pak"

I'll give it a try.

Thanks for the help.
--blueb
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Paul.

You could try this maybe??

Code: Select all

Procedure.l PackDir(SourceDir$,filename$)
 
  pak$=SourceDir$+filename$+".pak"
  hPack=CreatePack(pak$)
  If hPack
    MessageRequester("Pack","Pack File: "+pak$,0)
    ClosePack()
    Else
    MessageRequester("Error","Could not create pack in: "+pak$,0)    
  EndIf
  
EndProcedure
 
 
PackDir("c:\BigJob\","BigJob")
PackDir("","BigJob")

This way you specify folder and filename. If folder does not exists, it will fail. If no folder specified.. it will use the folder the app is executed in.

----------
Visit the PB Resources Site at http://www.reelmediaproductions.com/pb
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by blueb.

You too fast Paul.
I guess a very generic procedure
would be: PackDir()
It would:
- find it's current directory
- parse the string so that just the last foldername existed
- use that to name the packfile
- add all files to to packfile
- erase all unpacked files

When you opened the folder, all you'd see is the
file called: Bigjob.pak
When you wanted to unzip this file you'd call: UnPack("BigJob.pak")
which would unzip these in the current directory.

HTH,
--blueb
Post Reply