![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java JavaScript Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(C) Control Stored ZIP Paths Using AppendFromDir and saveExtraPathSee more Zip Examples
This example demonstrates how the
The example adds the same local filesystem file three times, but stores it in the ZIP archive using three different paths. The local filesystem file is:
The resulting ZIP archive contains:
The example demonstrates:
The example also illustrates an important rule:
when Note: This example requires Chilkat v11.0.0 or greater.
#include <C_CkZip.h> void ChilkatSample(void) { BOOL success; HCkZip zip; BOOL saveExtraPath; success = FALSE; success = FALSE; zip = CkZip_Create(); // Initialize the Zip object and set the output filename. // The .zip file is not created until WriteZip or WriteZipAndClose is called. success = CkZip_NewZip(zip,"myZip.zip"); if (success == FALSE) { printf("%s\n",CkZip_lastErrorText(zip)); CkZip_Dispose(zip); return; } // This example adds the same local filesystem file three times, // each time using a different stored path in the ZIP archive. // // The local file is: // // /temp/a/hamlet.xml // // The ZIP will contain: // // hamlet.xml // a/hamlet.xml // temp/a/hamlet.xml // // ------------------------------------------------------------ // Case 1: // Add the file using only the filename. // // AddFile adds a reference to the file in the local filesystem. // The file is not read or compressed at this point. The referenced // file is consumed later when WriteZipAndClose is called. saveExtraPath = FALSE; success = CkZip_AddFile(zip,"/temp/a/hamlet.xml",saveExtraPath); if (success == FALSE) { printf("%s\n",CkZip_lastErrorText(zip)); CkZip_Dispose(zip); return; } // Because saveExtraPath = FALSE, only the filename is stored // in the ZIP archive: // // hamlet.xml // // ------------------------------------------------------------ // Case 2: // Store the path "a/hamlet.xml" in the ZIP archive. // // AppendFromDir specifies the base directory for relative file paths. // The AppendFromDir path itself is not stored in the ZIP. // // Because AppendFromDir is set to "/temp", the relative path // "a/hamlet.xml" refers to the local filesystem file: // // /temp/a/hamlet.xml // // Because saveExtraPath = TRUE, the relative path is stored // in the ZIP as: // // a/hamlet.xml // CkZip_putAppendFromDir(zip,"/temp"); saveExtraPath = TRUE; success = CkZip_AddFile(zip,"a/hamlet.xml",saveExtraPath); if (success == FALSE) { printf("%s\n",CkZip_lastErrorText(zip)); CkZip_Dispose(zip); return; } // ------------------------------------------------------------ // Case 3: // Store the path "temp/a/hamlet.xml" in the ZIP archive. // // AppendFromDir is changed to "/" so that the relative path // "temp/a/hamlet.xml" again refers to the same local filesystem file: // // /temp/a/hamlet.xml // // Because saveExtraPath = TRUE, the stored ZIP path is: // // temp/a/hamlet.xml // CkZip_putAppendFromDir(zip,"/"); saveExtraPath = TRUE; success = CkZip_AddFile(zip,"temp/a/hamlet.xml",saveExtraPath); if (success == FALSE) { printf("%s\n",CkZip_lastErrorText(zip)); CkZip_Dispose(zip); return; } // ------------------------------------------------------------ // Write the ZIP archive. // // This is where the referenced file is actually read from disk // and compressed into the .zip archive. // // The resulting ZIP contains the same file three times, // each with a different stored path: // // hamlet.xml // a/hamlet.xml // temp/a/hamlet.xml // success = CkZip_WriteZipAndClose(zip); if (success == FALSE) { printf("%s\n",CkZip_lastErrorText(zip)); CkZip_Dispose(zip); return; } printf("ZIP archive created successfully.\n"); CkZip_Dispose(zip); } |
||||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.