AddData()
Previous  Top  Next

:AddData() - Add or Update a single (virtual) File/Entry to the Zip File.

Syntax

:AddData(<cFileName>,  
         <cBuffer>,  
         <cFTime>,  
         <cFAttr>,  
        [<cComment>]) --> nError  

Parameters

<cFileName>  
<cFileName> specifies the File Name that should be used for the (File) Entry that is added to the Zip File. This should be a valid File Name, with or without full (or relative) Path information, but without any invalid characters that are not permitted in (Windows) File Names. All "\" backslash characters will be saved as "/" forward slash characters and will be automatically converted.  

<cBuffer>  
<cBuffer> contains the original, uncompressed Data that should be used for the (File) Entry that is added to the Zip File. This can be an empty string but the parameter cannot be omitted or NIL!  

<cFTime>  
<cFTime> specifies the Date Time Stamp (in FAT_Time_Date format) that should be used for the (File) Entry that is added to the Zip File. This can be any valid FAT_Time_Date value, including an artificial one, as it is created/converted by the XbZ_DateTime2FatTimeDate(dDate, cTime) function, but cannot be omitted or NIL!  

<cFAttr>  
<cFAttr> specifies the File Attribute (as a U2Bin()-converted numeric File Attribute (Bit Mask) value) that should be used for the (File) Entry that is added to the Zip File. This can be any valid File Attribute value, including an artificial one, as it is created/converted by the XbZ_FileAttrib2DosAttrib(cAttrib) function, but cannot be omitted or NIL!  

NOTE: The supported File Attributes are ONLY valid for MS DOS and Windows (FAT/VFAT/NTFS)!  

<cComment>  
<cComment> optionally contains the text that should be added to the (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) Entry 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) Entry 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
(Simulated OS Error Code) The Zip File was not opened in Write Mode!
 
 
Description

The specified (File) Entry <cFileName> will be added to the Zip File. If an Entry with the specified File Name <cFileName> already exists in the Zip File, that Entry will be replaced/updated, instead.  
 
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>!  
 
With this method it is possible to add real, existing, Files and Directories, as well as virtual (File) Entries, by manually setting the <cFileName>, <cBuffer>, <cFTime>, and <cFAttr>, as well as the optional <cComment> parameters to any desired value.  

Example

Create and open a new Zip File named "MyArchive.zip", and add or replace/update two (virtual) File Entries. The first Entry will be a (virtual) Directory, named "\FakeDir" with the current Date and Time as File Date/Time Stamp, and all possible File Attributes set, and a File Comment is also included. The second Entry will be a (virtual) File, named "FakeFile", which will be placed inside the (virtual) Directory "\FakeDir", with a File Date/Time Stamp of "11/22/2000 12:34:56" and the File Attributes "A", "S", and "H" set. It also includes a File Comment. The File will contain a small text string, that will be either stored or deflated. Then save and close the Zip File:  
 
oZip := XbZLibZip():New('MyArchive.zip', XBZ_OPEN_CREATE)  
if oZip:IsOpen(.t.)  
   cFTime := XbZ_DateTime2FatTimeDate(Date(), Time())  
   cFAttr := XbZ_FileAttrib2DosAttrib('SHARD')  
   cNote  := 'Fake Directory Entry'  
   oZip:AddData('\FakeDir', '', cFTime, cFAttr, cNote)  
 
   cFTime := XbZ_DateTime2FatTimeDate(StoD('20001122'), '12:34:56')  
   cFAttr := XbZ_FileAttrib2DosAttrib('ASH')  
   cText  := 'Any data that you want to compress in a string...')  
   cNote  := 'Fake File Entry'  
   oZip:AddData('\FakeDir\FakeFile', cText, cFTime, cFAttr, cNote)  
endif  
oZip:Close()