I'm basically trying to extract a Mac application from a zip file using QuaZip. The code I'm using is:
QuaZip zip("file.zip");
qDebug() << zip.open(QuaZip::mdUnzip);
QuaZipFile file(&zip);
QFile out("application.app");
out.open(QIODevice::WriteOnly);
for(bool f=zip.goToFirstFile(); f; f=zip.goToNextFile()) {
file.open(QIODevice::ReadOnly);
//same functionality as QIODevice::readData() -- data is a char*, maxSize is qint64
char c;
while (file.getChar(&c)) out.putChar(c);
file.close();
}
out.flush();
out.close();
zip.close();
If I try to start the extracted app, I get an error message saying I "can't open the Application, because the Classic-Environment is no longer supported." I tried to make the .app executable but it still didn't work. I don't know any other easy way to extract an application using Qt.
I extracted it with the normal archiver and the application worked.
I'm not super familiar with QuaZip, but it almost looks like you're trying to extract the whole archive to a single file called "application.app", which is pretty seriously wrong. Applications are actually folders, not files -- the Finder just displays the folder (which has a name ending in ".app") as if it were a single file.
If you can dig up an example of using QuaZip to extract a ZIP file, you should just be able to use that code largely unmodified (other than the __MACOSX metadata, possibly).
It may be easier to just call unzip from your app using QProcess or system().
unzip -q <path-to-zip> -d <path-to-destionation>
Related
I have been trying to create and write a file using an iPhone C++ compiler, but the file always ends up at a location called /tmp, which I do not have access to. Is there any way to use the iPhone to create and write a file to a specific path location?
I have tried to use the following code, but it does not create the file at that location:
#include <iostream>
#include <fstream>
int main(){
std::ofstream File("/private/var/mobile/Containers/Data/Application/8EDE34D7-84CE-4147-8126-8D1AF04717A1/Documents/my_folder/my_file.txt");
File << "Hello!";
File.close();
return 0;
}
Your code ignores state of your File object. It just blindly attempts to write into it so no one knows what went wrong when opening it. There are lot of things that can go wrong but you should add code to check its state on any platform.
Two most common things that can go wrong:
Is /private/var/mobile/Containers/Data/Application/8EDE34D7-84CE-4147-8126-8D1AF04717A1 certainly home directory of your app? You can not access files of another app, if you are trying to do that. Normally you should request your app home directory from operating system, with CFCopyHomeDirectoryURL or perhaps getenv works too:
std::string home = getenv("HOME");
If it is directory of your app then you still can not create files in directory my_folder if that directory inside Documents directory of your app does not exist. For that you need to use NSFileManager or perhaps mkdir from sys/stat.h works too.
I have googled a lot but dint find any relative solution for my problem.
PROBLEM: I want to open .MTS file and its working find if its available in any directory. But if its in any package then my QFileDialog is not able to look into that package and select those .MTS files.
CODE:
auto filePaths = QFileDialog::getOpenFileNames(this, "Open Video File", lastOpenedPath, "*.MTS;*.mov");
qDebug() << "File Paths " << filePaths;
Now the .MTS files created under AVCHD(Advanced Video Coding High Definition) package default in Sony & Panasonic HD Camera, and I want to import/select that .MTS files.
HINT: QFileDialog is able to import/select those .MTS files in Windows machine, but fail to import/select in mac machine.
Highly appreciated any thoughts.
Thanks.
Well, if I understand what you want to do properly, I'm not sure that it's possible in Qt alone.
It did turn out to be easier than I expected to simply call in to Cocoa and NSOpenPanel to achieve what I think you're looking for.
Sample project is at: https://github.com/NSGod/widgetsOpenFileDialogCocoa
Basically, I renamed mainwindow.cpp to mainwindow.mm, then added an #import <Cocoa/Cocoa.h>:
#import <Cocoa/Cocoa.h>
void MainWindow::on_openFileButton_clicked()
{
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
[openPanel setAllowedFileTypes:[NSArray arrayWithObjects:#"mts", #"mov", nil]];
[openPanel setAllowsMultipleSelection:YES];
[openPanel setTreatsFilePackagesAsDirectories:YES];
[openPanel setTitle:#"Open Video File"];
NSInteger result = [openPanel runModal];
QStringList stringList;
if (result == NSFileHandlingPanelOKButton) {
NSArray *URLs = [openPanel URLs];
NSLog(#"URLs == %#", URLs);
for (NSURL *URL in URLs) {
stringList += QString::fromNSString(URL.path);
}
// do something with stringList
qDebug() << "filePaths == " << stringList;
}
}
Included in the project is a fakeBundle.component directory, which will be treated as a bundle (or "package") by OS X. But by setting treatsFilePackagesAsDirectories to YES, you can have the NSOpenPanel treat it as a directory (which it really is, of course).
Here is an image showing how the Finder treats this fakeBundle.component directory as if it were a single file:
And here in the NSOpenPanel, it's being treated as a directory:
An OSX package is a:
File system directory that is normally displayed to the user by the Finder as if it were a single file. Such a directory may be the top-level of a directory tree of objects stored as files, or it may be other archives of files or objects for various purposes, such as installer packages, or backup archives.
This is akin to an .mst or .msi file on Windows. Just as with OSX packages you would not be able to open your specified file within one of these packages. Your system open dialog is in fact doing you a disservice by allowing you to see into them, as you cannot open said files.
Your work around is to copy the file out of the package externally to the program then open the copy of the file.
I have the following problem. I would like to open and save a string to file.
ofstream file("file");
file << "example" << endl;
file.close()
Rather simple stuff. The problem is... the code above doesn't seem to create any files in my project directory. I have tried to use "locate" in terminal to find my file - withoud any results. What is interesting - I can open the file using ifstream and read from it, without any particular problems.
What the heck is going on here? Where should I look for the file created?
P.S. On Windows everything works 100% fine.
in my project directory
Where did you read that this is where to look? We should collaborate and correct that resource.
I have tried to use locate in terminal
It probably hasn't been indexed yet.
You need to look in the current working directory of the terminal that started the program.
I've got a program that is going to have several resource files that the user can put somewhere on the computer that isn't in the same folder as the executable. How do I get open those files?
I've found lots of answers saying that the reason things aren't working is that the file isn't in the working directory. I've tried providing fully qualified paths:
ifstream str;
str.open("/home/millere/foo.txt")
but that was unsuccessful. I know the path was correct (copy and paste). I can't find any documentation on it, but I assume it has to be possible. (vim ~/foo.txt from anywhere other than ~ works, for example).
Assuming you meant to use ifstream instead of iostream, your code is correct. ifstream can use a path to a file as well as the name of a file in the working directory.
No exceptions are thrown if the file does not exist, but the fail bit is set. You should check for this before trying to do anything with the stream.
std::ifstream input("/home/bob/stuff.txt");
if (!input) std::cerr << "Could not open the file!" << std::endl;
else
{
// ...
}
If you still cannot extract data from the file, the problem is somewhere else in your code.
I had the same issue and quickly noticed that, open when trying to get from a difference folder, had a different source directory (if using Cmake, the one that was specified by the cmake). You can find out, what the ouput/input source directory is by doing
system("ls")
or
system("dir")
on windows to show the content of the current ouput/input directory.
I am working on figuring out how to use Xcode 4 to debug c++ projects.
I have basically copy pasted a working c++ executable that when compiled from the terminal ran fine.
However, i was thinking it might be nice to use Xcode for debugging. So I am trying to migrate the single .cpp file into Xcode as a command line tool.
I need to read in a file called numbers.txt (which I supply through a command line argument) which is located in my project directory, and then out put to a file (whose name I also specify as an argument.)
The problem I am running into is that the files that are supplied as command line arguments are failing to open.
ifstream in;
ofstream out;
in.open(argv[1]);
out.open(argv[2]);
I have checked to make sure that the arguments are being properly passed and are named correctly. The ifstream in is being supplied with `numbers.txt', which I want to open a text file that I already have.
However when I check to make sure the ifstream is open:
if(in.is_open() == false){
cerr << "Unable to open input file" << endl;
return 1;
}
I get the error.
I suspect this has something to do with how Xcode organizes the project.
my numbers.txt file is just sitting in the Xcode project folder, and I have only one .cpp class and one product, the executable.
anyone know what I am missing here?
The executable built by Xcode is in a different folder than the project. Passing in the name of the file without an absolute path before it will cause the executable to look for it in the wrong place, which is why it can't be found. Some of the possible solutions are to include the file as part of the build process (so it ends up in the same directory as the executable) or to pass the file to be opened by its absolute path. There are other ways to solve the problem, too, but hopefully that should be enough to get you started.
Old thread, but i have faced the same problem now, and it is easy to solve. Just copy the file in the build phase.
Here is an screenshot of the final result (note the destination, subpath and checkbox):