Skip to content

Commit

Permalink
CompressingFile
Browse files Browse the repository at this point in the history
  • Loading branch information
cppcloud committed Jun 30, 2024
1 parent f580557 commit 81f69ed
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 0 deletions.
23 changes: 23 additions & 0 deletions 4-4-CompressingFile/4-4-CompressingFile.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
QT -= gui

CONFIG += c++11 console
CONFIG -= app_bundle

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
main.cpp

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
134 changes: 134 additions & 0 deletions 4-4-CompressingFile/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#include <QCoreApplication>
#include <QDebug>
#include <QFile>
#include <QDir>
#include <QBuffer>
#include <QTextStream>

//Our own custom file format, will not work with anything else!!

bool makeFile(QString path) {

QFile file(path);
if(file.open(QIODevice::WriteOnly)) {

QByteArray data;
for(int i = 0; i < 1000; i++) {
data.append(QString::number(i) + "\r\n");
}

file.write(data);

file.close();
return true;
}

return false;
}

QByteArray getHeader() {
QByteArray header;
header.append("@!~!@");
return header;
}

bool compressFile(QString originalFile, QString newFile) {
QFile ofile(originalFile);
QFile nfile(newFile);
QByteArray header = getHeader();

if(!ofile.open(QIODevice::ReadOnly)) return false;
if(!nfile.open(QIODevice::WriteOnly)) return false;
int size = 1024;

while (!ofile.atEnd()) {
QByteArray buffer = ofile.read(size);
QByteArray compressed = qCompress(buffer);
qInfo() << "Header at:" << nfile.pos();
nfile.write(header);
qInfo() << "Size: " << nfile.write(compressed); // unknown size

}
ofile.close();
nfile.close();
return true;
}

bool decompressFile(QString originalFile, QString newFile) {
QFile ofile(originalFile);
QFile nfile(newFile);
QByteArray header = getHeader();
int size = 1024;

if(!ofile.open(QIODevice::ReadOnly)) return false;
if(!nfile.open(QIODevice::WriteOnly)) return false;

//Double check that WE compressed the file
QByteArray buffer = ofile.peek(size);
if(!buffer.startsWith(header)) {
qCritical() << "We did not create this file!";
ofile.close();
nfile.close();
return false;
}

//Find the header positions
ofile.seek(header.length());
qInfo() << "Starting at: " << ofile.pos();

while (!ofile.atEnd()) {
buffer = ofile.peek(size);
qint64 index = buffer.indexOf(header);
qInfo() << "Header found at:" << index;

if(index > -1) {
//Have the header!
qint64 maxbytes = index;
qInfo() << "Reading: " << maxbytes;
buffer = ofile.read(maxbytes);
ofile.read(header.length());
} else {
//Do not have the header!
qInfo() << "Read all, no header";
buffer = ofile.readAll();
}

QByteArray decompressed = qUncompress(buffer);
qInfo() << "Decompressed: " << nfile.write(decompressed);
nfile.flush();
}

ofile.close();
nfile.close();

return true;

}

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

QString orginalFile = QDir::currentPath() + QDir::separator() + "orginal.txt";
QString compressedFile = QDir::currentPath() + QDir::separator() + "compressed.txt";
QString decompressedFile = QDir::currentPath() + QDir::separator() + "decompressed.txt";

if(makeFile(orginalFile)) {
qInfo() << "Original created";

if(compressFile(orginalFile, compressedFile)) {
qInfo() << "File compressed";
if(decompressFile(compressedFile,decompressedFile)) {
qInfo() << "File decompressed!";
} else {
qInfo() << "Could not decompress file!";
}
} else {
qInfo() << "File not compressed";
}


}

return a.exec();
}

0 comments on commit 81f69ed

Please sign in to comment.