Jump to content
Sign in to follow this  
CoderDunn

Andy's File Packer 2.0

Recommended Posts

CoderDunn

I'm currently taking an intermediate C++ course at college, and I wrote this file packer/unpacker for an assignment to showcase reading/writing binary files in C++. I figure some one here might have a use for it, so I converted it to a DLL and wrote the wrapper functions so you can use it in AutoIt3. Since I'm really bad at naming things, I'm calling it "Andy's File Packer" or "AFP" for short. I'm also using that as the file extension, but you can use any extension you want.

Why use this over FileInstall? Well you can use a variable for the file name, and can extract all the files with a single loop, which is nice. Especially when you have a bunch of files. Unfortunately, it doesn't support compression. However, you could FileInstall() the AFP file and then extract it.

Another advantage is that when you open an AFP file, only the embedded file table is loaded in memory. So no matter how big the AFP file is, it shouldn't use much memory (unless of course you pack thousands of files). All of the embedded files can be accessed and unpacked in any order.

Example scripts are included to show how to pack and unpack files. It's pretty simple. The C++ source code for the DLL is included, and since I wrote this for a college course, practically every other line is commented.

Here is a list of the functions:

; #FUNCTION# ====================================================================================================================
; Name...........: _AFP_CreateNew
; Description ...: Creates a new AFP file. Add files with _AFP_AddItem() and then compile the AFP with _AFP_Compile()
; Syntax.........: _AFP_CreateNew($dll, $filePath)
; Parameters ....: $dll - Handle to Andy's File Packer dll via DLLOpen()
;                  $filePath  - Path to the AFP file you wish to create
; Return values .: Success - 1
;                  Failure - 0, sets @error and @extended (when needed)
;                  |1 - Error creating character array struct to hold the file name
;                  |2 - DLLCall encountered an error, more specific DLL error code is in @extended
;                  |3 - DLL function CreateAFPFile() encountered an error, specific error code is in @extended
;                  |    @extended error codes:
;                  |        0 - Unable to open output file ($filePath)
; Author ........: Andrew Dunn (forums: CoderDunn)
; ===============================================================================================================================
Func _AFP_CreateNew($dll, $filePath)


; #FUNCTION# ====================================================================================================================
; Name...........: _AFP_AddItem
; Description ...: Adds a file to the list of files that will be embedded in an AFP file (created via _AFP_CreateNew()) when
;                  _AFP_Compile() is called
; Syntax.........: _AFP_AddItem($dll, $descript, $type, $filePath)
; Parameters ....: $dll - Handle to Andy's File Packer dll via DLLOpen()
;                  $descript - User defined string that describes the to-be-embedded file
;                  $type - User defined integer that describes the to-be-embedded file's type
;                  $filePath - Path to the file that will be embedded inside the AFP file
; Return values .: Success - 1
;                  Failure - 0, sets @error and @extended (when needed)
;                  |1 - $filePath does not exist
;                  |2 - Error creating character array struct to hold the description
;                  |3 - Error creating character array struct to hold the file path
;                  |4 - $type is not an integer
;                  |5 - DLLCall encountered an error, more specific DLL error code is in @extended
;                  |6 - DLL function _AFP_AddItem() encountered an error, specific error code is in @extended
;                  |    @extended error codes:
;                  |        0 - AFP file handle is closed or AFP file was opened via _AFP_Open()
; Author ........: Andrew Dunn (forums: CoderDunn)
; ===============================================================================================================================
Func _AFP_AddItem($dll, $descript, $type, $filePath)


; #FUNCTION# ====================================================================================================================
; Name...........: _AFP_Compile
; Description ...: Takes all the files added via _AFP_AddItem() and packs them into a single AFP file
; Syntax.........: _AFP_Compile($dll, $bool_CloseHandle)
; Parameters ....: $dll - Handle to Andy's File Packer dll via DLLOpen()
;                  $bool_CloseHandle - Boolean, use True if you want the AFP file handle to be closed when it's done being written
; Return values .: Success - 1
;                  Failure - 0, sets @error and @extended (when needed)
;                  |1 - $bool_CloseHandle is not a boolean
;                  |2 - DLLCall encountered an error, more specific DLL error code is in @extended
;                  |3 - DLL function _AFP_Compile() encountered an error, specific error code is in @extended
;                  |    @extended error codes:
;                  |        0 - AFP file handle is closed or AFP file was opened via _AFP_Open()
;                  |       -1 - Error while writing the file header
;                  |       -2 - Error while writing the embedded file table
;                  |       -3 - Error while writing the embedded file's data (one of the files added via _AFP_AddItem() doesn't exist,
;                               or is inaccessible)
; Author ........: Andrew Dunn (forums: CoderDunn)
; ===============================================================================================================================
Func _AFP_Compile($dll, $bool_CloseHandle = True)


; #FUNCTION# ====================================================================================================================
; Name...........: _AFP_Open
; Description ...: Opens an AFP file that was previously created via _AFP_CreateNew()
; Syntax.........: _AFP_Open($dll, $filePath)
; Parameters ....: $dll - Handle to Andy's File Packer dll via DLLOpen()
;                  $filePath - Path to the AFP file you wish to open
; Return values .: Success - 1
;                  Failure - 0, sets @error and @extended (when needed)
;                  |1 - Error creating character array struct to hold the file name
;                  |2 - DLLCall encountered an error, more specific DLL error code is in @extended
;                  |3 - DLL function OpenAFPFile() encountered an error, specific error code is in @extended
;                  |    @extended error codes:
;                  |        0 - Unable to open input file ($filePath)
;                  |       -1 - Unknown or corrupted file
;                  |       -2 - Error while reading the file header
;                  |       -3 - Error while reading the embedded file table
; Author ........: Andrew Dunn (forums: CoderDunn)
; ===============================================================================================================================
Func _AFP_Open($dll, $filePath)


; #FUNCTION# ====================================================================================================================
; Name...........: _AFP_GetNumItems
; Description ...: Returns the number of embedded files within the AFP opened via _AFP_Open()
; Syntax.........: _AFP_GetNumItems($dll)
; Parameters ....: $dll - Handle to Andy's File Packer dll via DLLOpen()
; Return values .: Success - The number of embedded files within the AFP file
;                  Failure - 0, sets @error and @extended (when needed)
;                  |1 - DLLCall encountered an error, more specific DLL error code is in @extended
;                  |2 - DLL function GetNumberOfItems() encountered an error, specific error code is in @extended
;                  |    @extended error codes:
;                  |        -1 - AFP file handle is closed
; Author ........: Andrew Dunn (forums: CoderDunn)
; ===============================================================================================================================
Func _AFP_GetNumItems($dll)


; #FUNCTION# ====================================================================================================================
; Name...........: _AFP_GetItemInfo
; Description ...: Returns an array containing information about an embedded file within an AFP file that was opened via _AFP_Open()
; Syntax.........: _AFP_GetItemInfo($dll, $index)
; Parameters ....: $dll - Handle to Andy's File Packer dll via DLLOpen()
;                  $index - Index of the embedded file (starting at 0)
; Return values .: Success - A four element array containing the following:
;                               $returnArr[0] - Description
;                               $returnArr[1] - Type
;                               $returnArr[2] - Position of the embedded file within the AFP (starting after the embedded file table's postion)
;                               $returnArr[3] - Size of the embedded file
;                  Failure - 0, sets @error and @extended (when needed)
;                  |1 - Index is out of range or AFP file handle is closed
;                  |2 - DLL function GetItemInfo() encountered an error
;                  |3 - Error creating a struct from the returned pointer, DllStructCreate() error code is in @extended
;                  |4 - Error creating description (char array) struct from pointer, DllStructCreate() error code is in @extended
; Author ........: Andrew Dunn (forums: CoderDunn)
; ===============================================================================================================================
Func _AFP_GetItemInfo($dll, $index)


; #FUNCTION# ====================================================================================================================
; Name...........: _AFP_ExtractFile
; Description ...: Extracts a file from an AFP file that was opened via _AFP_Open()
; Syntax.........: _AFP_ExtractFile($dll, $index, $filePath)
; Parameters ....: $dll - Handle to Andy's File Packer dll via DLLOpen()
;                  $index - Index of the file that you want to extract (starting at 0)
;                  $filePath - The file that it will be extracted to (Note: If file exists, it will be over written)
; Return values .: Success - 1
;                  Failure - 0, sets @error and @extended (when needed)
;                  |1 - Error creating character array struct to hold the file name
;                  |2 - $index is not an integer
;                  |3 - DLLCall encountered an error, more specific DLL error code is in @extended
;                  |4 - DLL function ExtractFile() encountered an error, specific error code is in @extended
;                  |    @extended error codes:
;                  |        0 - AFP file handle is closed or AFP file was opened via _AFP_CreateNew()
;                  |       -1 - $index is out of bounds
;                  |       -2 - Error opening output file path
;                  |       -3 - Error reading embedded file data
;                  |       -4 - Error writing embedded file data to output file
; Author ........: Andrew Dunn (forums: CoderDunn)
; ===============================================================================================================================
Func _AFP_ExtractFile($dll, $index, $filePath)


; #FUNCTION# ====================================================================================================================
; Name...........: _AFP_Close
; Description ...: Closes the AFP file handle that is opened after calling either _AFP_CreateNew() or _AFP_Open()
; Syntax.........: _AFP_Close($dll)
; Parameters ....: $dll - Handle to Andy's File Packer dll via DLLOpen()
; Return values .: Success - 1
;                  Failure - 0, sets @error and @extended (when needed)
;                  |1 - DLLCall encountered an error, more specific DLL error code is in @extended
; Author ........: Andrew Dunn (forums: CoderDunn)
; ===============================================================================================================================
Func _AFP_Close($dll)

And for those interested, here is the binary format specifications for an AFP file

The AFP Binary File Format

Note for readability, I placed each separate piece on it's own line. However, in
a real AFP file, they would all be on the same line with no delimiters.

======= Start of AFP File =======

[Null terminated string header, "Andy's File Packer 2.0"]
[4 byte integer which holds the number of embedded files]

~ Begin Embedded File Table ~
[Embedded File 1 Description, null terminated string]
[Embedded File 1 Type, 4 byte integer]
[Embedded File 1 Position in AFP file, 4 byte integer]
[Embedded File 1 Size, 4 byte integer]

...

[Embedded File n Description, null terminated string]
[Embedded File n Type, 4 byte integer]
[Embedded File n Position in AFP file, 4 byte integer]
[Embedded File n Size, 4 byte integer]
~ End Embedded File Table ~

~ Start Embedded File Raw Data ~
[Embedded File 1 Raw Data]

...

[Embedded File n Raw Data]
~ End Embedded File Raw Data ~

======= End of AFP File =======

If you have any questions or run into any problems, let me know :idea:

Download Here

Edited by CoderDunn

Share this post


Link to post
Share on other sites
Beege

This is great! Very well documented. Thankyou for sharing! :idea:

Share this post


Link to post
Share on other sites
CoderDunn

This is great! Very well documented. Thankyou for sharing! :)

Thanks! :(

It took a while to put it all together :idea:

Share this post


Link to post
Share on other sites
mesale0077

http://www.coderdunn.com/download/AndysFilePacker.zip

link broken

new link plz

Share this post


Link to post
Share on other sites
JLogan3o13

Since the OP has not been active for the past 3 years, you're likely not going to get a response. Additionally, AutoIt has changed a lot in 3 years - things may not work the same as they once did. This is why we discourage resurrecting old posts. :)


√-1 2^3 ∑ π, and it was delicious!

How to get your question answered on this forum!

Share this post


Link to post
Share on other sites
UEZ

http://www.coderdunn.com/download/AndysFilePacker.zip

link broken

new link plz

Buyur birader: Andy's File Packer.zip

Slm,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
mesale0077

teşekkür ederim

sağol kardeş

a.s

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×