Hi All,
I got a requirement to zip two files and the folder path will be in the network with read write permission.
I am able to generate files in the same folder. But when I try to zip these files using the below code, I get an error message saying “ClrObject static method invocation error.” from the line ( zip = System.IO.Packaging.Package::Open(_zipFileName, System.IO.FileMode::Create);) Code uesd is given below.
// This method will put all files in _FileNameList to a zip file named _zipFileName
// if success, it will return _zipFileName, otherwise, return a empty string
static str zipFileGen(str _zipFileName, container _fileNameList)
{
int bytesRead, bufSize=40960, i, p;
str sourceFileName, destFileName;
System.IO.Packaging.Package zip; // the zip file
System.IO.Packaging.PackagePart part; // the file add to zip
System.Uri uri; // the file’s path and name in the zip
System.IO.FileStream fileStream;
System.IO.Stream zipFileStream;
System.Byte[] buf = new System.BytebufSize;
str errorMsg;
System.Exception ex;
;
// delete zip file if already exists
new InteropPermission(InteropKind::ClrInterop).assert();
if (WinApi::fileExists(_zipFileName))
WinApi::deleteFile(_zipFileName);
try
{
// new FileIOPermission(_zipFileName, #io_write).demand();
// open a new document (zip file)
zip = System.IO.Packaging.Package::Open(_zipFileName, System.IO.FileMode::Create);
for (i=1; i<=conLen(_fileNameList); i++)
{
// get the name of the source file that needs to be added to zip file
sourceFileName = conPeek(_fileNameList, i);
// Define the file name and path in the zip
p = strScan(sourceFileName, @’’, strLen(sourceFileName), -strLen(sourceFileName));
destFileName = @"." + subStr(sourceFileName, p+1, strLen(sourceFileName)-p);
uri = System.IO.Packaging.PackUriHelper::CreatePartUri(new System.Uri(destFileName, System.UriKind::Relative));
// Add a Document part to the Package
part = zip.CreatePart(uri, ‘text/plain’, System.IO.Packaging.CompressionOption::Maximum);
zipFileStream = part.GetStream();
// Copy the file’s content to the Document Part
fileStream = new System.IO.FileStream(sourceFileName, System.IO.FileMode::Open, System.IO.FileAccess::Read);
do
{
bytesRead = fileStream.Read(buf,0,bufSize);
zipFileStream.Write(buf,0,bytesRead);
}
while ( bytesRead>0 );
// close all streams used
fileStream.Close();
fileStream.Dispose();
zipFileStream.Close();
zipFileStream.Dispose();
}
// close the document (zip file)
zip.Close();
zip = null;
return _zipFileName;
}
catch(Exception::CLRError)
{
ex = CLRInterop::getLastException().GetBaseException();
error(ex.get_Message());
}
/* catch
{
errorMsg = WinApi::formatMessage(WinApi::getLastError());
if (zip!=null) zip.Close();
return ‘’;
}*/
CodeAccessPermission::RevertAssert();
}
Please help me.
Thanks in advance
Prasan