Skip to content

Instantly share code, notes, and snippets.

@beakr beakr/Zip.cpp
Created Sep 26, 2014

Embed
What would you like to do?
void Zip::Open(const char * file)
{
int error = 0;
// Allocate a buffer that holds the error message string from
// zip_error_to_str.
char buffer[250];
// Open the zip file. Essentially it writes data to the `z' struct
// that holds all the zip file data.
z = zip_open(file, ZIP_CREATE, &error);
// Check if z struct is NULL (no zip file data was gathered, so zip_open
// failed).
if (z == NULL) {
// Convert libzip error code to its corresponding error message
// string.
zip_error_to_str(buffer, sizeof(buffer), error, errno);
cerr << "ERROR: Failed to open zip file with trace:" << endl
<< " " << buffer << endl;
exit(1);
}
}
Zip::Zip(const char * file)
{
Open(file);
}
Zip::Zip(const char * file, const char * password)
{
Open(file);
if (zip_set_default_password(z, password) == -1) {
cerr << "ERROR: Failed to set password protection for zip file"
<< endl;
exit(1);
}
}
Zip::~Zip()
{
if (zip_close(z) == -1) {
// TODO: handle error well
cerr << "WARNING: Failed to close zip file properly" << endl;
}
}
void Zip::AddFile(const char * file, char * content)
{
// This basically creates a source of file data to be compressed
// into a file inside the zip.
struct zip_source * zs = zip_source_buffer(z,
content,
strlen(content),
0);
// Check if the zip source buffer could be allocated.
if (zs == NULL) {
cerr << "ERROR: Failed to create source buffer for file data"
<< endl;
exit(1);
}
// Create the file with its file data (the source buffer) and assign
// the return value to an index variable. The index is a 16-bit integer
// storing the location of the file in the zip.
int index = (int)zip_add(z, file, zs);
// If the index is less then zero the file doesn't have a location in
// the zip file, meaning the function failed.
if (index < 0) {
cerr << "ERROR: Failed to add file to zip" << endl;
exit(1);
}
}
void Zip::AddDir(const char * name)
{
zip_add_dir(z, name);
}
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cerrno>
#include <zip.h>
using namespace std;
/**
* @class Zip
* Utility class for fetching/creating zip files and altering the contents.
* libzip is used for a variety of useful features, such as
* password-protecting access to zipped files.
*/
class Zip
{
private:
/*
* Instance of the zip struct type from libzip. It contains basic data
* about the zip file (and its contents).
*/
struct zip * z;
/*
* This private function only exists because we need a way to share the zip
* opening code between multiple constructors. Also C++11 constructor inheritance
* is apparently being stupid.
*/
void Open(const char * file);
public:
/**
* @param file Name of the zip file you want to create/work with.
*
* Open a new zip file for performing functions on or creating.
*/
Zip(const char * file);
/**
* @param file Name of the zip file you want to create/work with.
* @param password Password to protect the zip file.
*
* Open a new zip file for performing functions on or creating.
* Set a default password for protecting files in the zip from
* modification/altering.
*/
Zip(const char * file, const char * password);
/**
* Close the zip file from performing operations on.
*/
~Zip();
/**
* @param file Name of the file to create _inside_ the zip file.
* @param content Contents of the file.
*
* Add a file with a name/contents to the zip.
*/
void AddFile(const char * file, char * content);
/**
* @param name Name of the directory.
*
* Add a directory inside the zip file.
*/
void AddDir(const char * name);
};
@ethanhs

This comment has been minimized.

Copy link

commented Jan 23, 2016

Is there a license for these? Preferably a MIT type license? Looks really useful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.