|
-
January 28th, 2003, 12:08 AM
#1
Help with ZLIB. What am I doing wrong??
I just cant get the following code to work. First off the ZLIB compressor isnt compressing/uncompressing correctly (the src is originally 680kb, compressed = 78kb, uncompressed = 80kb) and many times over the statement: inp.open("d:\\Test20.zip", ios::binary); in the "uncompress area" isnt able to open the file for some reason setting "inp" to 0. What am i doing wrong within the following code?
The zlib header, libz.lib, and zlib.dll are available.
---
// ZLIB.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include "zlib.h"
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
char *des, *src;
unsigned long desLen, tdesLen, srcLen, rlen;
std::ifstream inp;
std::ofstream oup;
/////////////////////////////////////////
// Compress Test20.exe to Test20.zip
/////////////////////////////////////////
inp.open("d:\\Test20.exe", ios::binary);
oup.open("d:\\Test20.zip", ios::binary);
srcLen=3000;
desLen=tdesLen=srcLen*1.001+12;
src=new char[srcLen];
des=new char[desLen];
inp.read(src,srcLen);
rlen=inp.gcount();
while(rlen)
{
compress2((Bytef *) des,&tdesLen,(Bytef *)src,rlen,2);
oup.write(des,tdesLen);
tdesLen=desLen;
inp.read(src,srcLen);
rlen=inp.gcount();
}
oup.close();
inp.close();
/////////////////////////////////////////
// Uncompress Test20.zip to Test20B.exe
/////////////////////////////////////////
inp.open("d:\\Test20.zip", ios::binary);
oup.open("d:\\Test20B.exe", ios::binary);
srcLen=3000;
desLen=tdesLen=srcLen*1.001+12;
inp.read(src,srcLen);
rlen=inp.gcount();
while(rlen)
{
uncompress((Bytef *) des,&tdesLen,(Bytef *)src,rlen);
oup.write(des,tdesLen);
tdesLen=desLen;
inp.read(src,srcLen);
rlen=inp.gcount();
}
oup.close();
inp.close();
return 0;
}
---
Last edited by quantass; January 28th, 2003 at 04:07 AM.
-
January 28th, 2003, 10:21 AM
#2
You can't store a file in a char array! chars are null terminated, if your file contains a null character you will get the behaviour you are experiancing. Store your files in BYTE arrays and use memcpy to copy.
-
January 28th, 2003, 10:55 AM
#3
If using a ifstream for more than one file like you are,
you need to clear the state ...
Code:
oup.close();
inp.close();
// add these lines ...
oup.clear();
inp.clear();
-
January 28th, 2003, 12:33 PM
#4
You can store whatever you want in a char array - even binary data - as long as you don't treat them like strings i.e. expect strlen/strcpy to work OK. Use can use memcpy on a char array without any problem.
There are a lot of APIs that use char*/const char* without the referenced data being a string - setsockopt, for example:
http://msdn.microsoft.com/library/de...tsockopt_2.asp
Dan
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|