 |
|
 |
Here goes a C++ port, in case someone is interested:
void TestZip(LPCTSTR sourceFolder, LPCTSTR targetFile)
{
CoInitialize(0);
try {
_tprintf(_T("Zipping folder \"%s\" to file \"%s\".\n"), sourceFolder, targetFile);
byte emptyzip [] = {80,75,5,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
FILE * fp = fopen(targetFile, "wb");
fwrite(emptyzip, 24, sizeof(byte), fp);
fclose(fp);
fp = NULL;
_tprintf(_T(" Empty ZIP file created.\n"));
sh32::IShellDispatch2Ptr shell;
shell.CreateInstance(__uuidof(sh32::Shell));
_bstr_t b(sourceFolder);
_variant_t varSrc (b);
sh32::FolderPtr srcFolder = shell->NameSpace(varSrc);
#ifdef _DEBUG
sh32::FolderItemsPtr items = srcFolder->Items();
_tprintf(_T(" There are %d files to zip in the source folder.\n"), items->Count);
#endif
_bstr_t b2(targetFile);
_variant_t varDest(b2);
sh32::FolderPtr destFile = shell->NameSpace(varDest);
_tprintf(_T(" Target is %ls\n"), (LPCWSTR) destFile->Title);
long lFlags = FOF_NOCONFIRMATION | FOF_NOERRORUI ;
destFile->CopyHere(_variant_t((IDispatch *)srcFolder, true), lFlags);
::Sleep(100);
}
catch (const _com_error& ex) {
_tprintf(_T("************* Error %08x"), ex.Error());
}
CoUninitialize();
}
I hope someone finds this useful.
Pablo.
"Accident: An inevitable occurrence due to the action of immutable natural laws." (Ambrose Bierce, circa 1899).
|
|
|
|
 |
|
 |
Pablo,
Thank you! Yes, this is very useful for those of us still living in C++.
I found it pretty easy to write the companion, TestUnzip. Skip creating the empty zip, and use srcFolder->Items() instead of srcFolder as the arg in the CopyHere statement.
-- Jim
|
|
|
|
 |
|
|
 |
|
 |
Using the internal library of Microsoft for zipping requires a license from Inner Media, Inc. to be legal.
|
|
|
|
 |
|
 |
Actually my requirement is different.For me the source and the destination are same.The zip file will be created in the same folder from where all the files and folder being copied into the empty zip file. For that error is coming as a message box showing "Cannot copy compressed(zipped)folder onto itself" But when I select the different destination path then it will create the correct zip. but I have to create the zip file in the source path itself.
Please provide the modified code with solution. Its quite urgent.
|
|
|
|
 |
|
 |
houseboat2009 wrote: Its quite urgent.
That's not a nice way to ask.
By now, I guess someone already told you how to do it. In case your colleagues didn't, here's how it goes.
- Zip your folder into a temporary file, in a temporary folder.
- Move your temporary file to the desired (source) folder.
- That's all.
Pablo.
"Accident: An inevitable occurrence due to the action of immutable natural laws." (Ambrose Bierce, circa 1899).
|
|
|
|
 |
|
 |
I need to be able to only compress files that were modified after a certain date. Is there a way to do this.
|
|
|
|
 |
|
 |
Yes but because this just uses Windows built in system you wont get that feature automatically. Instead you need to loop through the files using the .Net System.IO namespace classes to do the date checking yourself and then modify my code to add those files/folders one by one in the middle of the loop(s).
|
|
|
|
 |
|
 |
Would this code would zip the contents of the DirectoryInfo[] if you substituted it for the args[0]?
|
|
|
|
 |
|
 |
If DirectoryInfo[] contains a string path to the source folder to be zipped then yes it should work just the same as manually passing in a string path.
|
|
|
|
 |
|
 |
i want to know that what is the maximum size that can be zipped by this application. i have a image file of size 5.88Mb and some other files in the Folder, when i zip that folder only up to that file which size is 5.88 mb is zipped and not other files.
What i have to Do beacause if a remove that file then it worked fine but not with that file.
Please give me any solution so that all the files of that folder can be zipped.
Thanks
Anil Pandey
Anil Pandey
|
|
|
|
 |
|
 |
This code uses Windows to do the zipping so it can do anything Windows can do. However if you read through the comments of the other posters below you will see where we came up with bug fixes and other enhancements that are not in the article itself. You can also try downloading the version at http://GeraldGibson.net to get a newer version of the code that will probably fix your problem.
|
|
|
|
 |
|
 |
Thanks a lot for the app, the app works fine when I try to zip small files, like if I have 3 files each 50K size it works fine, but when I have 3 files (each 50MB) it break up in the middle and do not finish the zipping, I am guessing it has to do something with putting the thread to sleep, I tried changing the sleep time to 10000 from 1000, this time it finishes the zipping but not closing so my application is waiting to hear from this app and freezes, any idea how I can resolve this
appreciate your help
Sri
|
|
|
|
 |
|
 |
Please read through the comments at the bottom of this page as well as on this page...
http://www.codeproject.com/KB/cs/decompresswinshellapics.aspx[^]
There you will see that others had this same problem and we fixed it a couple different ways... One of these days I need to get this article updated with the fix that we came up with...
You can also try downloading the latest version at http://GeraldGibson.net
|
|
|
|
 |
|
 |
Got it worked, thanks a lot
|
|
|
|
 |
|
 |
Unzip giving following error
Exception.Message: The system cannot find the file specified.
STACK TRACE: at Shell32.ShellClass.NameSpace(Object vDir)
Why I am getting this error?
any help appreciated!
|
|
|
|
 |
|
 |
Make sure this file is in the directory with the .exe
Interop.Shell32.dll
|
|
|
|
 |
|
 |
This is a very helpful article, so first and foremost thank you Gerald.
I ran into a problem w/ the code when I tried copying a folder that included blank folders. The blank folders caused an error message.
To avoid this, pass the SrcFldr object directly to CopyHere
ORIGINAL CODE:
Shell32.FolderItems items = SrcFlder.Items();
DestFlder.CopyHere(items, 20);
UPDATED CODE:
DestFlder.CopyHere(SrcFlder, 20);
This has worked perfectly for me.
Kevin
|
|
|
|
 |
|
 |
Thanks for the update!
If you look through the comments on this article and my other zip article or look at the similiar article at http://geraldgibson.net you will see that another reader and I found the same problem and posted updates in the comments and then made a system that did work with empty folders... however you seem to have accomplished the same thing in about 1/30th the code... oh well thats how it goes sometimes!
Thanks again,
GG
|
|
|
|
 |
|
 |
I can't get this code or any variation to run since the Process.Start call throws an exception that it can't find the file. The Current AppDomain is the folder that I started my app out of and it obviously doesn't have those two executables. How is this supposed to work?
Puzzled.
- Lutz
|
|
|
|
 |
|
 |
I just downloaded the zip file included with this article and there are two zip.exe files in there.. just look around.
|
|
|
|
 |
|
 |
I want Password protected zip file is this example support password protedted file?
Shahzad Aslam
Software Engineer
Softech Systems Limited
Email: shehzadaslam@hotmail.com
|
|
|
|
 |
|
 |
1. the 2 code examples - are the related somehow ?
2. if i use the shell example and i need to know when the zipping ends, how can i do that? (is there anyway to get the thread name that it makes or something)
thanks
|
|
|
|
 |
|
 |
Hello,Thanks for excellent Code
Can I ask you 2 question?
1)I don't understand How works Unzip procedure..Can you make an example?
2)I need to know when zip process ends...How can I?
Thanks
Carlo Ingrassia
|
|
|
|
 |
|
|
 |