summaryrefslogblamecommitdiffstats
path: root/heimdall-frontend/source/GZipFile.cpp
blob: af75f39ab6cd06d7e3677ae0b937e77e42f90549 (plain) (tree)


















































                                                                                                          
#import "GZipFile.h"

using namespace HeimdallFrontend;

GZipFile::GZipFile(const QString& path) :
	file(path),
	gzFile(nullptr)
{
}

GZipFile::~GZipFile()
{
	Close();

	if (temporary)
	{
		file.remove();
	}
}

bool GZipFile::Open(Mode mode)
{
	if (!file.isOpen() && !file.open(mode == GZipFile::ReadOnly ? QFile::ReadOnly : QFile::WriteOnly))
	{
		return (false);
	}

	gzFile = gzdopen(file.handle(), mode == GZipFile::ReadOnly ? "rb" : "wb");
	return (gzFile != nullptr);
}

void GZipFile::Close()
{
	file.close();
	gzclose(gzFile);
}

int GZipFile::Read(void *buffer, int length)
{
	return (length >= 0 && !file.isWritable() ? gzread(gzFile, buffer, length) : -1);
}

bool GZipFile::Write(void *buffer, int length)
{
	return (length >= 0 && gzwrite(gzFile, buffer, length) == length);
}

qint64 GZipFile::Offset() const
{
	return gzoffset(gzFile);
}