Skip to content

Commit

Permalink
Always Zip files when exporting to QIF
Browse files Browse the repository at this point in the history
When having transactions with multiple commodities, a QIF file for each
commodity is created. With Android's Storage Access Framework we are
limited to one file, so now we always pack QIFs into a single ZIP file.

Fixes codinguser#696
  • Loading branch information
rivaldi8 committed Aug 2, 2017
1 parent 07a1470 commit c4f6a26
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.annotation.NonNull;

import org.gnucash.android.db.adapter.AccountsDbAdapter;
import org.gnucash.android.db.adapter.TransactionsDbAdapter;
import org.gnucash.android.export.ExportParams;
import org.gnucash.android.export.Exporter;
import org.gnucash.android.model.Commodity;
import org.gnucash.android.util.FileUtils;
import org.gnucash.android.util.PreferencesHelper;
import org.gnucash.android.util.TimestampHelper;

Expand All @@ -38,6 +40,7 @@
import java.io.OutputStreamWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

Expand All @@ -52,6 +55,8 @@
* @author Yongxin Wang <fefe.wyx@gmail.com>
*/
public class QifExporter extends Exporter{
private static final String TAG = "QifExporter";

/**
* Initialize the exporter
* @param params Export options
Expand Down Expand Up @@ -232,12 +237,20 @@ public List<String> generateExport() throws ExporterException {

/// export successful
PreferencesHelper.setLastExportTime(TimestampHelper.getTimestampFromNow());
return splitQIF(file);
List<String> exportedFiles = splitQIF(file);
return zipQifs(exportedFiles);
} catch (IOException e) {
throw new ExporterException(mExportParams, e);
}
}

@NonNull
private List<String> zipQifs(List<String> exportedFiles) throws IOException {
String zipFileName = getExportCacheFilePath() + ".zip";
FileUtils.zipFiles(exportedFiles, zipFileName);
return Collections.singletonList(zipFileName);
}

/**
* Splits a Qif file into several ones for each currency.
*
Expand Down
34 changes: 34 additions & 0 deletions app/src/main/java/org/gnucash/android/util/FileUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.gnucash.android.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
* Misc methods for dealing with files.
*/
public final class FileUtils {
public static void zipFiles(List<String> files, String zipFileName) throws IOException {
OutputStream outputStream = new FileOutputStream(zipFileName);
ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
byte[] buffer = new byte[1024];
for (String fileName : files) {
File file = new File(fileName);
FileInputStream fileInputStream = new FileInputStream(file);
zipOutputStream.putNextEntry(new ZipEntry(file.getName()));

int length;
while ((length = fileInputStream.read(buffer)) > 0) {
zipOutputStream.write(buffer, 0, length);
}
zipOutputStream.closeEntry();
fileInputStream.close();
}
zipOutputStream.close();
}
}

0 comments on commit c4f6a26

Please sign in to comment.