Skip to content

Commit

Permalink
Merge pull request #2125 from obfusk/stocard-spanned-fix
Browse files Browse the repository at this point in the history
StocardImporter: use ZipFile (fixes spanned archive marker breaking import)
  • Loading branch information
TheLastProject authored Oct 24, 2024
2 parents 20d8374 + 98f4e0d commit 088ee5d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

import com.google.zxing.BarcodeFormat;

import net.lingala.zip4j.ZipFile;
import net.lingala.zip4j.io.inputstream.ZipInputStream;
import net.lingala.zip4j.model.LocalFileHeader;
import net.lingala.zip4j.model.FileHeader;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
Expand All @@ -20,9 +21,7 @@
import org.json.JSONObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -130,11 +129,9 @@ public void importData(Context context, SQLiteDatabase database, File inputFile,
throw new FormatException("Issue parsing CSV data", e);
}

InputStream input = new FileInputStream(inputFile);
ZipInputStream zipInputStream = new ZipInputStream(input, password);
zipData = importZIP(zipInputStream, zipData);
zipInputStream.close();
input.close();
ZipFile zipFile = new ZipFile(inputFile, password);
zipData = importZIP(zipFile, zipData);
zipFile.close();

if (zipData.cards.keySet().size() == 0) {
throw new FormatException("Couldn't find any loyalty cards in this Stocard export.");
Expand All @@ -144,24 +141,24 @@ public void importData(Context context, SQLiteDatabase database, File inputFile,
saveAndDeduplicate(context, database, importedData);
}

public ZIPData importZIP(ZipInputStream zipInputStream, final ZIPData zipData) throws IOException, FormatException, JSONException {
public ZIPData importZIP(ZipFile zipFile, final ZIPData zipData) throws IOException, FormatException, JSONException {
Map<String, StocardRecord> cards = zipData.cards;
Map<String, StocardProvider> providers = zipData.providers;

String[] customProvidersBaseName = null;
String[] cardBaseName = null;
String customProviderId = "";
String cardName = "";
LocalFileHeader localFileHeader;
while ((localFileHeader = zipInputStream.getNextEntry()) != null) {
String fileName = localFileHeader.getFileName();
for (FileHeader fileHeader : zipFile.getFileHeaders()) {
String fileName = fileHeader.getFileName();
String[] nameParts = fileName.split("/");

if (nameParts.length < 2) {
continue;
}

String userId = nameParts[1];
ZipInputStream zipInputStream = zipFile.getInputStream(fileHeader);

if (customProvidersBaseName == null) {
// FIXME: can we use the points-account/statement/content.json balance info somehow?
Expand Down Expand Up @@ -302,6 +299,8 @@ record = new StocardRecord();
} else if (!fileName.endsWith("/")) {
Log.d(TAG, "Unknown or unused file " + fileName + ", skipping...");
}

zipInputStream.close();
}

return new ZIPData(cards, providers);
Expand Down
Binary file modified app/src/test/res/protect/card_locker/stocard.zip
Binary file not shown.
Binary file modified app/src/test/res/protect/card_locker/stocard2.zip
Binary file not shown.

0 comments on commit 088ee5d

Please sign in to comment.