Question:
how can I zip a file in c++?
Lizadel Lucauas
2010-04-20 12:18:27 UTC
I have to zip the files But I'm the one who has to write a program for it in c++
please give me some ideas
thanks
Four answers:
Cubbi
2010-04-20 13:25:21 UTC
There is no compression built into C++, you will have to use a suitable library. The most portable C++ library is boost -- http://www.boost.org .



Here's how to compress the file "test.txt" with boost:



#include

#include

#include

#include

#include

using namespace std;

using namespace boost::iostreams;

int main()

{

ifstream in("test.txt", ios_base::in | ios_base::binary);

ofstream file("test.z", ios_base::out | ios_base::binary);

filtering_streambuf out;

out.push(zlib_compressor());

out.push(file);

boost::iostreams::copy(in, out);

}



to compile, remember to link boost_iostreams library.



There are many other libraries that offer compression too.
Amishera A
2010-04-20 12:35:42 UTC
You can use the method call



system() or exec()



where you pass the parameters as the zip file program (eg zip), flags for that program and the file name.
keri
2016-09-13 09:43:16 UTC
Maybe, but I'm not 100%
anonymous
2016-08-05 19:41:38 UTC
Good question, hope you find the right answers


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...