-6

I am using a code snippet from this page on how to create a zip file and add and a compress a directory to that zip file. I am running the following on Windows 7 but it does not seem to create the zip file at all.

BSTR bstrFolderOutName = L"C:\\Test\\Archive.zip";
BYTE startBuffer[] = {80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
FILE *f = _wfopen(bstrFolderOutName, L"wb");
fwrite(startBuffer,sizeof(startBuffer),1,f);
fclose(f); 
Bill Walton
  • 811
  • 1
  • 16
  • 31
  • 1
    This shouldn't even *compile*. `bstrFolderOutName` is of type `BSTR` (unrelated: it should actually be `const wchar_t*` or `const WCHAR*` in this case), but is being assigned a non-wide string (i.e. no `L` prefix). And that code snippet link is horrid. That isn't the way to prime a BSTR for use in a VARIANT (or anywhere else for that matter; it should be using `SysAllocString()`. – WhozCraig May 01 '14 at 10:40
  • Apologies. I stuck that first line in there because the rest of the snippet is just part of a much larger method which takes the destination folder name as a parameter of type BSTR, tries to create the zip file and then add and compress the source folder. I have now added the L prefix. The rest of the code is unchanged. – Bill Walton May 01 '14 at 10:47

1 Answers1

2

The stated problem, that no file is created, is impossible to answer with the information given. It is most likely due to an invalid file path. However, the OP states in a comment that the path in his example is not the real code.


EDIT: the hex string example that I cited originally was wrong, I just tested.

This code works:

#include <stdio.h>

auto main() -> int
{
    FILE* f = fopen("foo.zip", "wb");
    //fwrite( "\x80\x75\x05\x06\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 22, 1, f );
    fwrite( "\x50\x4B\x05\x06\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 22, 1, f );
    fclose(f);
}

Harumph, one cannot even trust Stack Overflow comments. Not to mention accepted answers.


Original text:

Assuming that the OP now has edited the code so that the part below is the real code, then this constant

{80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

is not identical to

"\x80\x75\x05\x06\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"

Can the OP spot the relevant difference?

Further, given that, can the OP infer anything about his source of information?

My example from a comment elsewhere.

Community
  • 1
  • 1
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
  • One good thing, we *may* get [an accepted SO answer about this](http://stackoverflow.com/questions/118547/creating-a-zip-file-on-windows-xp-2003-in-c-c#comment35865236_118606) elsewhere, corrected. I'm just leaving it to the author to fix it. – Cheers and hth. - Alf May 01 '14 at 11:51
  • Thanks. That code is working on my target platform. I don't have much faith in the rest of the CodeProject article, though and the article in the other thread you linked to no longer exists so whether I'll be able to compress anything to the .zip file is another matter. – Bill Walton May 01 '14 at 12:39