Unicode C
Unicode C
Download a Zip from a URL and OpenBd. (No .zip file is created)
See more Zip Examples
Demonstrates how to download a .zip from a URL, opens the Zip, and gets the contents of a file. No file is ever written.Chilkat Unicode C Downloads
#include <C_CkHttpW.h>
#include <C_CkBinDataW.h>
#include <C_CkZipW.h>
#include <C_CkZipEntryW.h>
#include <C_CkStringBuilderW.h>
void ChilkatSample(void)
{
BOOL success;
HCkHttpW http;
HCkBinDataW bd;
HCkZipW zip;
HCkZipEntryW entry;
int lineEndingBehavior;
const wchar_t *xmlStr;
HCkStringBuilderW sb;
success = FALSE;
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http = CkHttpW_Create();
bd = CkBinDataW_Create();
// This URL is valid and can be tested...
success = CkHttpW_QuickGetBd(http,L"https://chilkatdownload.com/example_data/hamlet.zip",bd);
if (CkHttpW_getLastMethodSuccess(http) == FALSE) {
wprintf(L"%s\n",CkHttpW_lastErrorText(http));
CkHttpW_Dispose(http);
CkBinDataW_Dispose(bd);
return;
}
zip = CkZipW_Create();
// Open the zip from the bytes contained in bd.
success = CkZipW_OpenBd(zip,bd);
if (success == FALSE) {
wprintf(L"%s\n",CkZipW_lastErrorText(zip));
CkHttpW_Dispose(http);
CkBinDataW_Dispose(bd);
CkZipW_Dispose(zip);
return;
}
// Get the entry for the file we want..
entry = CkZipEntryW_Create();
success = CkZipW_EntryOf(zip,L"hamlet.xml",entry);
if (success == FALSE) {
wprintf(L"%s\n",CkZipW_lastErrorText(zip));
CkHttpW_Dispose(http);
CkBinDataW_Dispose(bd);
CkZipW_Dispose(zip);
CkZipEntryW_Dispose(entry);
return;
}
// Convert all line endings to CRLF (if needed)
lineEndingBehavior = 2;
xmlStr = CkZipEntryW_unzipToString(entry,lineEndingBehavior,L"utf-8");
if (CkZipEntryW_getLastMethodSuccess(entry) == FALSE) {
wprintf(L"%s\n",CkZipEntryW_lastErrorText(entry));
CkHttpW_Dispose(http);
CkBinDataW_Dispose(bd);
CkZipW_Dispose(zip);
CkZipEntryW_Dispose(entry);
return;
}
// The XML in this case is about 274K, so let's just examine the last 20 lines...
sb = CkStringBuilderW_Create();
CkStringBuilderW_Append(sb,xmlStr);
wprintf(L"%s\n",CkStringBuilderW_lastNLines(sb,20,TRUE));
CkHttpW_Dispose(http);
CkBinDataW_Dispose(bd);
CkZipW_Dispose(zip);
CkZipEntryW_Dispose(entry);
CkStringBuilderW_Dispose(sb);
}