I'm using Qt Creator in a new project, so I don't know many things about this... :(
I want to download a zip file, containing a json file, read this file and use that information. I can download the zip, save it in my disk and open it again to read json and use it. But I want to open my zip just in memory without really saving it...
I have the zip info in a QByteArray and I need to send this "file" to QuaZip constructor/object.
How do I do it?
You can use QBuffer. It provides a QIODevice interface for a QByteArray.
Example:
QByteArray byteArray("abc");
QBuffer buffer(&byteArray);
buffer.open(QIODevice::WriteOnly);
buffer.seek(3);
buffer.write("def", 3);
buffer.close();
Then you can use QuaZip::QuaZip(QIODevice *ioDevice) constructor to create QuaZip object.
Related
I'm developing an application, but I need it to save its information onto the computer, and load it from there next time it's opened.
To give the simplest example: I have an array of strings and I want to save them as a *.txt file in the application's directory. And every member of the array should be on a new row of the file.
And I want to load the entries of the file into the array when I open the app, or create an empty *.txt file, if one doesn't exist.
Note: if there is an easier way to do this, instead of saving them into a *.txt, please tell me. Saving them strictly as a *.txt format isn't mandatory.
Also, I am using wxWidgets for my application, if it's gonna make it any easier.
MainFrame::MainFrame() {
wxFileName f(wxStandardPaths::Get().GetExecutablePath());
wxString appPath(f.GetPath());
std::ifstream inputFileStream;
inputFileStream.open(std::string(appPath.mb_str(wxConvUTF8)) + "data.txt");
std::string data;
inputFileStream >> data;
}
MainFrame::~MainFrame()
{
wxFileName f(wxStandardPaths::Get().GetExecutablePath());
wxString appPath(f.GetPath());
std::ofstream outputFileStream;
outputFileStream.open(std::string(appPath.mb_str(wxConvUTF8)) + "data.txt");
std::string data = "something";
outputFileStream << data;
outputFileStream.close();
}
When frame is created, I get the data. When frame is destroyed, I save the data. I don't use C++ standard library classes, but wxWidgets classes and methods for UTF-8 support. (I haven't checked if this piece of code works – it's taken from my old project.)
Using QByteArray QIODevice::readAll() from QT5, I was able to make a bytes array from a txt file or an image, used decode after and recreated the file correctly. But, when I tried with a .db file (SQLITE) it didn't work.
I noticed that when you open a .db with a text editor, you will see "SQLite format 3" followed by encoded characters. After making a QByteArray from a .db file, followed by decode() to recreate the file, when I opened it with a text editor, the file only contains the text "SQLite format 3".
Does QByteArray only work with txt file or Image file?
If it does, how can I make a Array of bytes from a .db (SQLITE) file.
Thanks
Update1 (The code belows works):
QFile file("C:/database.db");
if(!file.open(QIODevice::ReadOnly))
qDebug()<<"You are stupid!";
QByteArray byteArray = file.readAll();
QFile file2("C:/database2.db");
file2.open(QIODevice::WriteOnly);
file2.write(byteArray);
file2.close();
file.close();
Update2:
About the decode I mentioned in my initial question, I was using the following:
QString QFile::decodeName(const QByteArray & localFileName)
which make no sense when you read carefully the documentation and was just wrong. :)
You should not open that file with QIODevice::Text flag.
Check this http://doc.qt.io/qt-5/qiodevice.html#OpenModeFlag-enum
Can we use snappy file as input file to Map reduce application without using a Custom Input Class?
I was not able to find any information about this.
Regards,
Nish
Yes you can do that but you may have to decompress the file first to use it.
CompressionCodec codec = (CompressionCodec) ReflectionUtils.newInstance(SnappyCodec.class, new Configuration());
After creating an instance of codec, call createInputStream and pass the hadoop path.
codec.createInputStream
I have a QFile that needs to be sent through a LAN network. In order to do so, I convert the QFile into QByteArray by doing:
//! [Inside a QTcpSocket class]
// Get the file name using a QFileDialog
QFile file(QFileDialog::getOpenFileName(NULL, tr("Upload a file")));
// If the selected file is valid, continue with the upload
if (!file.fileName().isEmpty) {
// Read the file and transform the output to a QByteArray
QByteArray ba = file.readAll();
// Send the QByteArray
write(ba);
}
When I receive it, I can transform it easily by using:
void saveFile(QByteArray ba) {
// Ask the user where he/she wants to save the file
QFile file(QFileDialog::getSaveFileName(NULL, tr("Save file")));
// Check that the path is valid
if (!file.fileName().isEmpty()) {
// Write contents of ba in file
file.write(ba);
// Close the file
file.close();
}
}
However, I would like to know the file name (such as Document.docx) or at least know its extension to avoid forcing the user to know exactly which file type he has received.
Ideally, when the file is uploaded, the receiver user will be prompted to save the file. For example:
Sender sends Document1.docx
Receiver gets prompted if he/she wants to save Document1.docx
Based on receiver's decision, Document1.docx is saved in receiver's workstation.
So, my question is: Is there any way to know the name and extension of a QFile when its transformed into a QByteArray and then transformed again (in another computer) into a QFile?
You just read the raw bytes of a file using QFile::readAll().
Q: Is there any way to know the name and extension of a QFile when its
transformed into a QByteArray?
No. The file name and extension is not necessarily inserted in bytes of a file. It depends on the file format. For example you can make your custom file with a specific extension and put its name and extension at the begging of the bytes.
You can send the name and extension of the file manually before sending the raw bytes of the file. Before that you can send the length of name and extension and the number of bytes in file. This way you know how many bytes are related to name, extension and raw bytes.
The answer on you particular question is NO. Although particular files contain the so called magic numbers (or signatures) in the beginning of the data, this signature should be unique for particular file format.
But the problem you have described seems not be serious, because you can simply send filename string and extension string separately of the file content.
I'm trying to compress a text file with QT:
QFile inFile("d:\\build\\Directories\\Debug\\files\\developer.txt");
bool open_file_result = inFile.open(QIODevice::ReadOnly);
QByteArray ba = inFile.readAll();
QFile file("d:\\build\\Directories\\Debug\\files\\developer.gz");
bool open_zip_result = file.open(QIODevice::WriteOnly);
QDataStream out(&file);
out << qCompress(ba);
file.close();
open_file_result and open_zip_result are both true. I can also see the zip file (it also has a size, such as 50KB) but I can't open it. I'm getting the following error:
The archive is either in unknown format or damaged.
What am I doing wrong? If you have a better/another way to compress a text file, please tell me!
There is a difference between the Zip compression algorithm and the Zip container. You're confusing the two.
You need to do some research into the Zip container format that will help you locate and extract a zipped file within a Zip container. Once you've gotten that file, you can apply qUncompress to that file only.
Try looking at the QuaZip, which is a library that has been written for this purpose.