bool uncompress(const ustring & archive, const ustring & directory) // Uncompresses "archive" into "directory". // Returns whether this was successful. { // Bail out if the archive was not recognized. if (!compressed_archive_recognized(archive)) { gw_critical(_("cannot uncompress unrecognized archive")); return false; } // Ensure that the output directory is there. gw_mkdir_with_parents (directory); // Get the uncompression identifier. int uncompression_identifier = uncompression_identifier_get (archive); // Do the uncompression. int result = -1; switch (uncompression_identifier) { case 0: { gw_message("I'm not yet smart enough to handle the " + archive + " file type"); break; } case 1: { // If you have a zip utility installed in Windows, etc. GwSpawn spawn (Directories->get_unzip()); spawn.arg ("-o"); // overwrite without prompting if (!directory.empty ()) { spawn.arg ("-d"); // extract files into exdir spawn.arg (directory); } spawn.arg (archive); spawn.progress (_("Unpacking"), false); spawn.run (); result = 0; break; } case 2: { GwSpawn spawn(Directories->get_tar()); spawn.arg ("--force-local"); // to permit : in filename (like C:\Users\...) spawn.arg("-xvzf"); // x=eXtract, z=gunZip, f=Filename to extract if (!directory.empty()) { spawn.workingdirectory(directory); } spawn.arg(archive); spawn.progress(_("Unpacking"), false); spawn.run(); result = spawn.exitstatus; DEBUG("tar return code="+std::to_string(result)); break; } } // Return whether things were ok. return (result == 0); }
void ImportAssistant::on_button_files () // Selection and processing of the files to import. { // Set directory. ustring directory; if (!files_names.empty()) { directory = gw_path_get_dirname (files_names[0]); } // Processing variables. files_messages.clear(); files_book_ids.clear(); // Select files. { vector <ustring> files = gtkw_file_chooser_open_multiple (assistant, "", directory); if (!files.empty()) { files_names = files; } } // Ensure that there are only uncompressed files, or only one compressed file. vector <ustring> compressed_files; for (unsigned int i = 0; i < files_names.size(); i++) { if (compressed_archive_recognized (files_names[i])) compressed_files.push_back (files_names[i]); } if (!compressed_files.empty()) { if (compressed_files.size() > 1) { files_messages.push_back (_("You have selected more than one compressed file")); } if (compressed_files.size() == 1) { if (files_names.size() != 1) { files_messages.push_back (_("You have selected a mixture of normal and compressed files")); } } } // Optionally uncompress the archive and let the user select files from within it. if (files_messages.empty()) { if (compressed_files.size() == 1) { ustring unpack_directory = gw_build_filename (Directories->get_temp (), "uncompress"); unix_rmdir (unpack_directory); uncompress (compressed_files[0], unpack_directory); gtkw_dialog_info (assistant, _("You will now be asked to select files from within the compressed archive")); files_names = gtkw_file_chooser_open_multiple (assistant, "", unpack_directory); } } // Check that all files are in Unicode. if (files_messages.empty()) { vector <ustring> unicode_files; vector <ustring> non_unicode_files; for (unsigned int i = 0; i < files_names.size(); i++) { ustring contents; gchar *s; g_file_get_contents(files_names[i].c_str(), &s, NULL, NULL); contents = s; g_free(s); if (contents.validate()) { unicode_files.push_back (files_names[i]); } else { non_unicode_files.push_back (files_names[i]); } } files_names = unicode_files; if (!non_unicode_files.empty()) { files_messages.push_back (_("The following files are not in the right Unicode format and are therefore not fit for import:")); for (unsigned int i = 0; i < non_unicode_files.size(); i++) { files_messages.push_back (non_unicode_files[i]); } files_messages.push_back (_("The online help provides more information about how to make these fit for use.")); } } // Check that at least one file was selected. if (files_messages.empty()) { if (files_names.empty()) { files_messages.push_back (_("No files were selected")); } } // Specific checks for each import type. if (files_messages.empty()) { switch (get_type ()) { case itBible: { switch (get_bible_type()) { case ibtUsfm: { import_check_usfm_files (files_names, files_book_ids, bible_name, files_messages); break; } case ibtBibleWorks: { import_check_bibleworks_file (files_names, files_book_ids, bible_name, files_messages); break; } case ibtOnlineBible: { break; } case ibtRawText: { break; } } break; } case itReferences: { break; } case itStylesheet: { break; } case itNotes: { break; } case itKeyterms: { break; } } } // Gui update. on_assistant_prepare (vbox_files); }