Improved Image Downloader (but still zipper not working on Android)
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -54,6 +54,81 @@ public class ImgDownloader {
|
|||||||
return contentBuilder.toString();
|
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 {
|
public static String DownloadCardImages(String set, String[] availableSets, String targetres, String basePath, String destinationPath) throws IOException {
|
||||||
String res = "";
|
String res = "";
|
||||||
|
|
||||||
@@ -427,9 +502,13 @@ public class ImgDownloader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
/*try {
|
/*try {
|
||||||
Zipper appZip = new Zipper(destinationPath + set + "/");
|
//Zipper appZip = new Zipper(destinationPath + set + "/");
|
||||||
appZip.generateFileList(new File(destinationPath + set + "/"));
|
//appZip.generateFileList(new File(destinationPath + set + "/"));
|
||||||
appZip.zipIt(destinationPath + set + ".zip");
|
//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) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}*/
|
}*/
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -441,15 +441,15 @@ public class SDLActivity extends Activity implements OnKeyListener
|
|||||||
}
|
}
|
||||||
|
|
||||||
availableSets = new String[sets.size() + 1];
|
availableSets = new String[sets.size() + 1];
|
||||||
availableSets[0] = "*.*";
|
availableSets[0] = "*.* - All Wagic sets (thousands of cards)";
|
||||||
for (int i = 1; i < availableSets.length; i++){
|
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()
|
cardDownloader.setSingleChoiceItems(availableSets, -1, new DialogInterface.OnClickListener()
|
||||||
{
|
{
|
||||||
public void onClick(DialogInterface dialog, int item)
|
public void onClick(DialogInterface dialog, int item)
|
||||||
{
|
{
|
||||||
set = availableSets[item];
|
set = availableSets[item].split(" - ")[0];
|
||||||
downloadCardStarting(set);
|
downloadCardStarting(set);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user