diff --git a/projects/mtg/Android/libs/J7Zip.jar b/projects/mtg/Android/libs/J7Zip.jar new file mode 100644 index 000000000..b17091a83 Binary files /dev/null and b/projects/mtg/Android/libs/J7Zip.jar differ diff --git a/projects/mtg/Android/libs/commons-compress-1.18.jar b/projects/mtg/Android/libs/commons-compress-1.18.jar new file mode 100644 index 000000000..e401046b5 Binary files /dev/null and b/projects/mtg/Android/libs/commons-compress-1.18.jar differ diff --git a/projects/mtg/Android/libs/xz-1.6.jar b/projects/mtg/Android/libs/xz-1.6.jar new file mode 100644 index 000000000..140b981f9 Binary files /dev/null and b/projects/mtg/Android/libs/xz-1.6.jar differ diff --git a/projects/mtg/Android/src/net/wagic/utils/ImgDownloader.java b/projects/mtg/Android/src/net/wagic/utils/ImgDownloader.java index cb1d9fbc1..206b0d691 100644 --- a/projects/mtg/Android/src/net/wagic/utils/ImgDownloader.java +++ b/projects/mtg/Android/src/net/wagic/utils/ImgDownloader.java @@ -54,6 +54,81 @@ public class ImgDownloader { 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 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 = ""; @@ -427,9 +502,13 @@ public class ImgDownloader { } } /*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(); }*/ diff --git a/projects/mtg/Android/src/net/wagic/utils/SevenZ.java b/projects/mtg/Android/src/net/wagic/utils/SevenZ.java new file mode 100644 index 000000000..61b08d7b6 --- /dev/null +++ b/projects/mtg/Android/src/net/wagic/utils/SevenZ.java @@ -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); + } +} diff --git a/projects/mtg/Android/src/org/libsdl/app/SDLActivity.java b/projects/mtg/Android/src/org/libsdl/app/SDLActivity.java index 7c4cd8e3f..397254b0a 100644 --- a/projects/mtg/Android/src/org/libsdl/app/SDLActivity.java +++ b/projects/mtg/Android/src/org/libsdl/app/SDLActivity.java @@ -441,15 +441,15 @@ public class SDLActivity extends Activity implements OnKeyListener } 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); } });