Skip to content

Commit

Permalink
Improved Image Downloader (but still zipper not working on Android)
Browse files Browse the repository at this point in the history
  • Loading branch information
valfieri committed Aug 21, 2019
1 parent 2ac3ec6 commit 1b88f7d
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 6 deletions.
Binary file added projects/mtg/Android/libs/J7Zip.jar
Binary file not shown.
Binary file not shown.
Binary file added projects/mtg/Android/libs/xz-1.6.jar
Binary file not shown.
85 changes: 82 additions & 3 deletions projects/mtg/Android/src/net/wagic/utils/ImgDownloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,81 @@ private static String readLineByLineJava8(String filePath) {
return contentBuilder.toString();
}

public static String getSetInfo(String setName, boolean zipped, String path){
String cardsfilepath = "";
boolean todelete = false;
if(zipped){
File resFolder = new File(path + File.separator);
File [] listOfFile = resFolder.listFiles();
ZipFile zipFile = null;
InputStream stream = null;
java.nio.file.Path filePath = null;
try {
for (int i = 0; i < listOfFile.length; i++){
if (listOfFile[i].getName().contains(".zip")){
zipFile = new ZipFile(path + File.separator + listOfFile[i].getName());
break;
}
}
if(zipFile == null)
return "";
Enumeration<? extends ZipEntry> e = zipFile.entries();
while (e.hasMoreElements()) {
ZipEntry entry = e.nextElement();
String entryName = entry.getName();
if(entryName.contains("sets/")){
if(entryName.contains("_cards.dat")){
String[] names = entryName.split("/");
if(setName.equalsIgnoreCase(names[1])){
stream = zipFile.getInputStream(entry);
byte[] buffer = new byte[1];
java.nio.file.Path outDir = Paths.get(path + File.separator);
filePath = outDir.resolve("_cards.dat");
try {
FileOutputStream fos = new FileOutputStream(filePath.toFile());
BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length);
int len;
while ((len = stream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
fos.close();
bos.close();
cardsfilepath = filePath.toString();
todelete = true;
} catch (Exception ex) {}
break;
}
}
}
}
} catch (IOException ioe){ }
finally {
try {
if (zipFile!=null) {
zipFile.close();
}
} catch (IOException ioe) {}
}
} else {
File setFolder = new File(path + File.separator + "sets" + File.separator + setName + File.separator);
cardsfilepath = setFolder.getAbsolutePath() + File.separator + "_cards.dat";
}
String lines = readLineByLineJava8(cardsfilepath);
if(todelete) {
File del = new File(cardsfilepath);
del.delete();
}
int totalcards = 0;
String findStr = "total=";
int lastIndex = lines.indexOf(findStr);
String totals = lines.substring(lastIndex, lines.indexOf("\n", lastIndex));
totalcards = Integer.parseInt(totals.split("=")[1]);
findStr = "name=";
lastIndex = lines.indexOf(findStr);
String name = lines.substring(lastIndex, lines.indexOf("\n", lastIndex)).split("=")[1];
return name + " (" + totalcards + " cards)";
}

public static String DownloadCardImages(String set, String[] availableSets, String targetres, String basePath, String destinationPath) throws IOException {
String res = "";

Expand Down Expand Up @@ -427,9 +502,13 @@ else if(scryset.equalsIgnoreCase("ZVE"))
}
}
/*try {
Zipper appZip = new Zipper(destinationPath + set + "/");
appZip.generateFileList(new File(destinationPath + set + "/"));
appZip.zipIt(destinationPath + set + ".zip");
//Zipper appZip = new Zipper(destinationPath + set + "/");
//appZip.generateFileList(new File(destinationPath + set + "/"));
//appZip.zipIt(destinationPath + set + ".zip");
//File setFolder = new File(destinationPath + set + "/");
//File[] filesToZip = setFolder.listFiles();
//SevenZ zip = new SevenZ();
//zip.addFilesToZip(setFolder, new File(destinationPath + set + ".zip"));
} catch (Exception e) {
e.printStackTrace();
}*/
Expand Down
74 changes: 74 additions & 0 deletions projects/mtg/Android/src/net/wagic/utils/SevenZ.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package net.wagic.utils;

import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.compress.archivers.ArchiveOutputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.apache.commons.compress.utils.IOUtils;

import java.io.*;
import java.util.Collection;
import java.util.Enumeration;



public class SevenZ {

/**
* Add all files from the source directory to the destination zip file
*
* @param source the directory with files to add
* @param destination the zip file that should contain the files
* @throws IOException if the io fails
* @throws ArchiveException if creating or adding to the archive fails
*/
public void addFilesToZip(File source, File destination) throws IOException, ArchiveException {
OutputStream archiveStream = new FileOutputStream(destination);
ArchiveOutputStream archive = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, archiveStream);

File[] fileList = source.listFiles();
for (int i = 0; i < fileList.length; i++) {
File file = fileList[i];
if(!file.isDirectory()){
String entryName = getEntryName(source, file);
ZipArchiveEntry entry = new ZipArchiveEntry(entryName);
archive.putArchiveEntry(entry);
BufferedInputStream input = new BufferedInputStream(new FileInputStream(file));
IOUtils.copy(input, archive);
input.close();
archive.closeArchiveEntry();
} else {
File[] subfileList = file.listFiles();
for (int j = 0; j < subfileList.length; j++) {
File subfile = subfileList[j];
String entryName = getEntryName(source, subfile);
ZipArchiveEntry entry = new ZipArchiveEntry(entryName);
archive.putArchiveEntry(entry);
BufferedInputStream input = new BufferedInputStream(new FileInputStream(subfile));
IOUtils.copy(input, archive);
input.close();
archive.closeArchiveEntry();
}
}
}

archive.finish();
archiveStream.close();
}

/**
* Remove the leading part of each entry that contains the source directory name
*
* @param source the directory where the file entry is found
* @param file the file that is about to be added
* @return the name of an archive entry
* @throws IOException if the io fails
*/
private String getEntryName(File source, File file) throws IOException {
int index = source.getAbsolutePath().length() + 1;
String path = file.getCanonicalPath();

return path.substring(index);
}
}
6 changes: 3 additions & 3 deletions projects/mtg/Android/src/org/libsdl/app/SDLActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -441,15 +441,15 @@ private void downloadCardImages()
}

availableSets = new String[sets.size() + 1];
availableSets[0] = "*.*";
availableSets[0] = "*.* - All Wagic sets (thousands of cards)";
for (int i = 1; i < availableSets.length; i++){
availableSets[i] = sets.get(i-1);
availableSets[i] = sets.get(i-1) + " - " + ImgDownloader.getSetInfo(sets.get(i-1), true, getSystemStorageLocation());
}
cardDownloader.setSingleChoiceItems(availableSets, -1, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int item)
{
set = availableSets[item];
set = availableSets[item].split(" - ")[0];
downloadCardStarting(set);
}
});
Expand Down

0 comments on commit 1b88f7d

Please sign in to comment.