EDN Admin
Well-known member
Hi,
I have compressed xml files. they are compressed using a C++ application with a <span style="white-spacere zlib-compression library.the
output is a compressed file with a generic extension - *.trz
i need to decompress the trz files in my C# application.
have tried doing so with ZLIB.NET library and the DotNetZip library.
the only progress i had was with DotNetZip, but it would hang while outputting the decompressed stream to the output file (maybe because it couldnt find the EOF ?)
I searched the web for answers but got to dead ends.
what approach do you think i should take to solve this matter , taking into account i dont have a choice in how the files are being compressed ?
should i use a C++ managed DLL (that does the reverse and decompresses ) and call it from my application ?
this is my C# code for decompressing the files using Ionic.Zlib
<pre> protected void Decompress_Click(object sender, EventArgs e)
{
String path = @"c:\temp\new.xml";
decompressFile(userBasePath + "compressed.trz", path);
}
public static void decompressFile(string inFile, string outFile)
{
System.IO.FileStream outFileStream = new System.IO.FileStream(outFile, System.IO.FileMode.Create);
zlib.ZOutputStream outZStream = new zlib.ZOutputStream(outFileStream);
System.IO.FileStream inFileStream = new System.IO.FileStream(inFile, System.IO.FileMode.Open);
try
{
CopyStream(inFileStream, outZStream);
}
finally
{
outZStream.Close();
outFileStream.Close();
inFileStream.Close();
}
}
public static void CopyStream(System.IO.Stream input, System.IO.Stream output)
{
byte[] buffer = new byte[2000];
int len;
while ((len = input.Read(buffer, 0, 2000)) > 0)
{
output.Write(buffer, 0, len);
}
output.Flush();
}
[/code]
this is the C++ code for decompressing the files.
<pre>/*
* Decompress:
* Extracts the contents of a zlib-compressed file into a temporary file.
* Parameters:
* CString, path of compressed file to extract
* Return value:
* CString, path of extracted file
* Throws:
* CException*
*/
BOOL CImport:ecompress(CString szSource, CString& szDest)
{
CString szTempPath, sMsg;
BYTE* pBufIn, * pBufOut;
UINT nRead;
ULONG nOrgSize, nCompSize;
int nResult;
pBufIn = NULL;
pBufOut = NULL;
// get temporary path
GetTempPath(_MAX_PATH, szTempPath.GetBuffer(_MAX_PATH));
szTempPath.ReleaseBuffer();
// create temporary file name
GetTempFileName(szTempPath, "XML", 0, szDest.GetBuffer(_MAX_PATH));
szDest.ReleaseBuffer();
try
{
// open source (zipped) file
CFile fileSrc(szSource, CFile::modeRead);
// open destination (extracted) file
CFile fileDest(szDest, CFile::modeWrite |
CFile::modeCreate);
// allocate input and output buffers
fileSrc.Seek(-(long)sizeof(ULONG), CFile::end);
fileSrc.Read(&nOrgSize, sizeof(ULONG));
nCompSize = fileSrc.GetLength() - sizeof(ULONG);
pBufIn = new BYTE[nCompSize];
pBufOut = new BYTE[nOrgSize];
if(pBufIn == NULL || pBufOut == NULL)
AfxThrowMemoryException();
// read source file
fileSrc.SeekToBegin();
nRead = fileSrc.Read(pBufIn, nCompSize);
if(nRead != nCompSize)
AfxThrowFileException(CFileException::generic);
// uncompress
nResult = uncompress(pBufOut, &nOrgSize, pBufIn, nCompSize);
// write to destination file
if(nResult == Z_OK)
fileDest.Write(pBufOut, nOrgSize);
else
AfxMessageBox(IDS_FAIL_DECOMPRESS);
fileSrc.Close();
fileDest.Close();
} // try
catch(CException* e)
{
TCHAR szError[255];
e->GetErrorMessage(szError, 255);
sMsg.Format(_T("Exception in func Decompress: %s"), szError);
theApp.WriteToLog(sMsg);
throw e;
} // catch
// clean up
delete pBufIn;
delete pBufOut;
return nResult == Z_OK;
} // Decompress[/code]
any ideas anyone ?
Thanks
View the full article
I have compressed xml files. they are compressed using a C++ application with a <span style="white-spacere zlib-compression library.the
output is a compressed file with a generic extension - *.trz
i need to decompress the trz files in my C# application.
have tried doing so with ZLIB.NET library and the DotNetZip library.
the only progress i had was with DotNetZip, but it would hang while outputting the decompressed stream to the output file (maybe because it couldnt find the EOF ?)
I searched the web for answers but got to dead ends.
what approach do you think i should take to solve this matter , taking into account i dont have a choice in how the files are being compressed ?
should i use a C++ managed DLL (that does the reverse and decompresses ) and call it from my application ?
this is my C# code for decompressing the files using Ionic.Zlib
<pre> protected void Decompress_Click(object sender, EventArgs e)
{
String path = @"c:\temp\new.xml";
decompressFile(userBasePath + "compressed.trz", path);
}
public static void decompressFile(string inFile, string outFile)
{
System.IO.FileStream outFileStream = new System.IO.FileStream(outFile, System.IO.FileMode.Create);
zlib.ZOutputStream outZStream = new zlib.ZOutputStream(outFileStream);
System.IO.FileStream inFileStream = new System.IO.FileStream(inFile, System.IO.FileMode.Open);
try
{
CopyStream(inFileStream, outZStream);
}
finally
{
outZStream.Close();
outFileStream.Close();
inFileStream.Close();
}
}
public static void CopyStream(System.IO.Stream input, System.IO.Stream output)
{
byte[] buffer = new byte[2000];
int len;
while ((len = input.Read(buffer, 0, 2000)) > 0)
{
output.Write(buffer, 0, len);
}
output.Flush();
}
[/code]
this is the C++ code for decompressing the files.
<pre>/*
* Decompress:
* Extracts the contents of a zlib-compressed file into a temporary file.
* Parameters:
* CString, path of compressed file to extract
* Return value:
* CString, path of extracted file
* Throws:
* CException*
*/
BOOL CImport:ecompress(CString szSource, CString& szDest)
{
CString szTempPath, sMsg;
BYTE* pBufIn, * pBufOut;
UINT nRead;
ULONG nOrgSize, nCompSize;
int nResult;
pBufIn = NULL;
pBufOut = NULL;
// get temporary path
GetTempPath(_MAX_PATH, szTempPath.GetBuffer(_MAX_PATH));
szTempPath.ReleaseBuffer();
// create temporary file name
GetTempFileName(szTempPath, "XML", 0, szDest.GetBuffer(_MAX_PATH));
szDest.ReleaseBuffer();
try
{
// open source (zipped) file
CFile fileSrc(szSource, CFile::modeRead);
// open destination (extracted) file
CFile fileDest(szDest, CFile::modeWrite |
CFile::modeCreate);
// allocate input and output buffers
fileSrc.Seek(-(long)sizeof(ULONG), CFile::end);
fileSrc.Read(&nOrgSize, sizeof(ULONG));
nCompSize = fileSrc.GetLength() - sizeof(ULONG);
pBufIn = new BYTE[nCompSize];
pBufOut = new BYTE[nOrgSize];
if(pBufIn == NULL || pBufOut == NULL)
AfxThrowMemoryException();
// read source file
fileSrc.SeekToBegin();
nRead = fileSrc.Read(pBufIn, nCompSize);
if(nRead != nCompSize)
AfxThrowFileException(CFileException::generic);
// uncompress
nResult = uncompress(pBufOut, &nOrgSize, pBufIn, nCompSize);
// write to destination file
if(nResult == Z_OK)
fileDest.Write(pBufOut, nOrgSize);
else
AfxMessageBox(IDS_FAIL_DECOMPRESS);
fileSrc.Close();
fileDest.Close();
} // try
catch(CException* e)
{
TCHAR szError[255];
e->GetErrorMessage(szError, 255);
sMsg.Format(_T("Exception in func Decompress: %s"), szError);
theApp.WriteToLog(sMsg);
throw e;
} // catch
// clean up
delete pBufIn;
delete pBufOut;
return nResult == Z_OK;
} // Decompress[/code]
any ideas anyone ?
Thanks
View the full article