AddFile()
Previous  Top  Next

:AddFile() - Add or Update a single File from Disk to the Zip File.

Syntax

:AddFile(<cFileName>,  
        [<cRootDir>],  
        [<cComment>]) --> nError  

Parameters

<cFileName>  
<cFileName> is the exact File Name of the File or Directory that should be compressed and added to the Zip File, including any Path information that should be saved, but without any illegal or wild card characters.  

<cRootDir>  
<cRootDir> optionally specifies the Directory from which the file should be read. The default value is the Current Directory of the Current Drive.  

<cComment>  
<cComment> optionally specifies the text that should be added to the Zip File Entry as a File Comment. The default value is "" (an empty string) for New Entries, or the existing File Comment for Existing (updated) Entries.  

Returns

This method returns an Error Code that indicates if the Adding/Replacing of the File was successful or not. Possible values are the following Define Constants from the XbZLib.ch file:  
 
Define
Value
Description
XBZ_OK
0
Adding/Updating of File was successful.
XBZ_STREAM_ERROR
-2
(ZLib Error Code) Compression Level invalid.
XBZ_MEM_ERROR
-4
(ZLib Error Code) Not enough Memory.
XBZ_BUF_ERROR
-5
(ZLib Error Code) Output Buffer too small.
ERROR_FILE_NOT_FOUND
2
(OS Error Code) File could not be found, or the Zip File was not in Write Mode, or it was tried to either add the Zip File itself or its Log File to the Zip File!
 
 
Additionally, all other OS Error Codes that might be created while trying to read the contents of the specified file are also possible. OS Error Codes are always greater than "0"!  

Description

The specified File <cFileName> will be read from the specified directory <cRootDir>, compressed (if necessary), and then added to the open Zip File. If an entry with the specified File Name <cFileName> already exists in the Zip File, then that entry will be replaced/updated. Otherwise, a new entry will be added to the end of the Zip File.  
 
The Zip File itself -- as well as the possibly specified Log File -- will not be added to the Zip File -- unless they appear to be located on a different drive, due to the selected drive names or paths.  
 
It is not possible to have more than one entry with the same File Name in a Zip File! All File Names are considered case-insensitive when they are compared to find already existing entries, even though the File Names are saved case-sensitive, exactly as specified in <cFileName>, which may include a (relative or absolute) Path Name!  
 
If the specified File cannot be found or opened, or the contents of the File could not be compressed correctly, an error will be raised, and the File will NOT be added to the Zip File!  
 
NOTE: The supported File Attributes are ONLY valid for MS DOS and Windows (FAT/VFAT/NTFS)!  

Example

Add the File "D:\Data\Test.dbf" twice to the Zip File "MyArchive.zip", first with the File Name "Test.dbf", then again with the File Name "Data\Test.dbf" (which includes a relative Path Name). If either File Name exists in the open Zip File, the existing File Entry will be replaced, otherwise a new File Entry will be added. The File Comments for the two File Entries are also set. Then save and close the updated Zip File:  
 
oZip:New('MyArchive.zip')  
if oZip:IsOpen(.t.)  
   oZip:AddFile('Test.dbf', 'D:\Data', 'File Name only')  
   oZip:AddFile('Data\Test.dbf', 'D:\', 'With relative Path')  
endif  
oZip:Close()