Skip to content

Commit 5630786

Browse files
committed
Export QIF as zip files so as to include multiple files generated for multicurrency transactions
1 parent 7132435 commit 5630786

2 files changed

Lines changed: 40 additions & 11 deletions

File tree

app/src/main/java/org/gnucash/android/export/ExportAsyncTask.java

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
package org.gnucash.android.export;
1919

20-
import android.annotation.TargetApi;
2120
import android.app.Activity;
2221
import android.app.ProgressDialog;
2322
import android.content.Context;
@@ -27,7 +26,6 @@
2726
import android.database.sqlite.SQLiteDatabase;
2827
import android.net.Uri;
2928
import android.os.AsyncTask;
30-
import android.os.Build;
3129
import android.preference.PreferenceManager;
3230
import android.support.annotation.NonNull;
3331
import android.support.v4.content.FileProvider;
@@ -79,6 +77,8 @@
7977
import java.util.Date;
8078
import java.util.List;
8179
import java.util.concurrent.TimeUnit;
80+
import java.util.zip.ZipEntry;
81+
import java.util.zip.ZipOutputStream;
8282

8383
/**
8484
* Asynchronous task for exporting transactions.
@@ -280,21 +280,38 @@ private void moveExportToUri() throws Exporter.ExporterException {
280280
return;
281281
}
282282

283-
//we only support exporting to a single file
284-
String exportedFile = mExportedFiles.get(0);
285-
try {
286-
moveFile(exportedFile, mContext.getContentResolver().openOutputStream(exportUri));
287-
} catch (IOException e) {
288-
e.printStackTrace();
289-
Log.e(TAG, "Error moving export file to: " + exportUri);
290-
Crashlytics.logException(e);
283+
if (mExportedFiles.size() > 0){
284+
try {
285+
OutputStream outputStream = mContext.getContentResolver().openOutputStream(exportUri);
286+
ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
287+
byte[] buffer = new byte[1024];
288+
for (String exportedFile : mExportedFiles) {
289+
File file = new File(exportedFile);
290+
FileInputStream fileInputStream = new FileInputStream(file);
291+
zipOutputStream.putNextEntry(new ZipEntry(file.getName()));
292+
293+
int length;
294+
while ((length = fileInputStream.read(buffer)) > 0) {
295+
zipOutputStream.write(buffer, 0, length);
296+
}
297+
zipOutputStream.closeEntry();
298+
fileInputStream.close();
299+
}
300+
zipOutputStream.close();
301+
} catch (IOException ex) {
302+
Log.e(TAG, "Error when zipping QIF files for export");
303+
ex.printStackTrace();
304+
Crashlytics.logException(ex);
305+
}
291306
}
292307
}
293308

294309
/**
295310
* Move the exported files to a GnuCash folder on Google Drive
296311
* @throws Exporter.ExporterException
312+
* @deprecated Explicit Google Drive integration is deprecated, use Storage Access Framework. See {@link #moveExportToUri()}
297313
*/
314+
@Deprecated
298315
private void moveExportToGoogleDrive() throws Exporter.ExporterException {
299316
Log.i(TAG, "Moving exported file to Google Drive");
300317
final GoogleApiClient googleApiClient = BackupPreferenceFragment.getGoogleApiClient(GnuCashApplication.getAppContext());
@@ -421,7 +438,9 @@ private void moveExportToOwnCloud() throws Exporter.ExporterException {
421438
* Moves the exported files from the internal storage where they are generated to
422439
* external storage, which is accessible to the user.
423440
* @return The list of files moved to the SD card.
441+
* @deprecated Use the Storage Access Framework to save to SD card. See {@link #moveExportToUri()}
424442
*/
443+
@Deprecated
425444
private List<String> moveExportToSDCard() throws Exporter.ExporterException {
426445
Log.i(TAG, "Moving exported file to external storage");
427446
new File(Exporter.getExportFolderPath(mExporter.mBookUID));

app/src/main/java/org/gnucash/android/ui/export/ExportFormFragment.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,17 @@ private void selectExportFile() {
501501
Intent createIntent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
502502
createIntent.setType("text/*").addCategory(Intent.CATEGORY_OPENABLE);
503503
String bookName = BooksDbAdapter.getInstance().getActiveBookDisplayName();
504-
createIntent.putExtra(Intent.EXTRA_TITLE, Exporter.buildExportFilename(mExportFormat, bookName));
504+
505+
if (mExportFormat == ExportFormat.XML || mExportFormat == ExportFormat.QIF) {
506+
createIntent.setType("application/zip");
507+
}
508+
509+
String filename = Exporter.buildExportFilename(mExportFormat, bookName);
510+
if (mExportTarget == ExportParams.ExportTarget.URI && mExportFormat == ExportFormat.QIF){
511+
filename += ".zip";
512+
}
513+
514+
createIntent.putExtra(Intent.EXTRA_TITLE, filename);
505515
startActivityForResult(createIntent, REQUEST_EXPORT_FILE);
506516
}
507517

0 commit comments

Comments
 (0)