diff --git a/ComicReader/src/com/blogspot/applications4android/comicreader/ActivityComicReader.java b/ComicReader/src/com/blogspot/applications4android/comicreader/ActivityComicReader.java index f5bfcd1..c94f459 100755 --- a/ComicReader/src/com/blogspot/applications4android/comicreader/ActivityComicReader.java +++ b/ComicReader/src/com/blogspot/applications4android/comicreader/ActivityComicReader.java @@ -286,13 +286,12 @@ public class ActivityComicReader extends ComicActivity { try { mList.storeSelected(); File src = mList.selectedFile(); - File dst = new File(FileUtils.getSdcard(), "backup_selected.json"); - dst.delete(); - if(FileUtils.copyFile(src, dst)) { - msg = "Successfully backed up 'My Comics' list to '" + dst.getPath() + "'"; + String zipFilename = FileUtils.getSdcard() + "/backup_favComics.zip"; + if (FileUtils.favSave(mList, src.getPath(), zipFilename)) { + msg = "Successfully backed up 'My Comics' list to '" + zipFilename + "'"; } else { - msg = "Failed to backup 'My Comics' list to '" + dst.getPath() + "'"; + msg = "Failed to backup 'My Comics' list to '" + zipFilename + "'"; } } catch(Exception e) { @@ -306,7 +305,6 @@ public class ActivityComicReader extends ComicActivity { { final File dst = mList.selectedFile(); dst.delete(); - final File src = new File(FileUtils.getSdcard(), "backup_selected.json"); AlertDialog.Builder alertbox = new AlertDialog.Builder(ActivityComicReader.this); alertbox.setMessage(res.getString(R.string.my_comic_restore_confirmation)); alertbox.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { @@ -317,11 +315,12 @@ public class ActivityComicReader extends ComicActivity { alertbox.setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface arg0, int arg1) { String msg; - if(FileUtils.copyFile(src, dst)) { - msg = "Successfully restored 'My Comics' list from '" + src.getPath() + "' to" + dst.getPath(); + String zipFilename = FileUtils.getSdcard() + "/backup_favComics.zip"; + if (FileUtils.favLoad(zipFilename)) { + msg = "Successfully restored 'My Comics' list from '" + zipFilename + "' to" + FileUtils.getComicRoot(); } else { - msg = "Failed to restore 'My Comics' list from '" + src.getPath() + "'"; + msg = "Failed to restore 'My Comics' list from '" + zipFilename + "'"; } Toast.makeText(ActivityComicReader.this, msg, Toast.LENGTH_LONG).show(); GetComicsTask get_task = new GetComicsTask(); diff --git a/ComicReader/src/com/blogspot/applications4android/comicreader/core/Comic.java b/ComicReader/src/com/blogspot/applications4android/comicreader/core/Comic.java index dfa3395..c98b871 100755 --- a/ComicReader/src/com/blogspot/applications4android/comicreader/core/Comic.java +++ b/ComicReader/src/com/blogspot/applications4android/comicreader/core/Comic.java @@ -406,7 +406,11 @@ public abstract class Comic extends ComicParser { } return mCurrent.hasText(); } - + + public boolean containsFavs(){ + return mFavs.size() > 0 ? true : false; + } + /** * Creates a valid file name out of the strip's title * diff --git a/ComicReader/src/com/blogspot/applications4android/comicreader/core/FileUtils.java b/ComicReader/src/com/blogspot/applications4android/comicreader/core/FileUtils.java index 696a02a..6b5ae7a 100755 --- a/ComicReader/src/com/blogspot/applications4android/comicreader/core/FileUtils.java +++ b/ComicReader/src/com/blogspot/applications4android/comicreader/core/FileUtils.java @@ -1,5 +1,7 @@ package com.blogspot.applications4android.comicreader.core; +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; @@ -13,9 +15,13 @@ import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.Reader; +import java.util.ArrayList; import java.util.Random; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; +import java.util.zip.ZipOutputStream; -import com.blogspot.applications4android.comicreader.exceptions.ComicSDCardFull; +import org.json.JSONException; import android.content.res.AssetManager; import android.os.Environment; @@ -23,6 +29,9 @@ import android.os.StatFs; import android.text.format.Time; import android.util.Log; +import com.blogspot.applications4android.comicreader.exceptions.ComicNotFoundException; +import com.blogspot.applications4android.comicreader.exceptions.ComicSDCardFull; + /** * Class containing helper methods for working on files */ @@ -293,4 +302,140 @@ public class FileUtils { } } + /** + * saves the list to a zip file to the sdcard + * + * @param mList the list of comics. + * @param listLocation Location of where the selected.json are + * @param zipName the name of the zip file (path as well) + * @return + */ + public static boolean favSave(ComicClassList mList, String listLocation, String zipName) { + ArrayList files = new ArrayList(); + files.add(listLocation); + for (int cI : mList.getSelectedComicList()) { + try { + Comic c = mList.getComicFromIndex(cI); + c.readProperties(); + if (c.containsFavs()) { + String fileName = getComicRoot() + "/props/" + c.getName() + ".json"; + files.add(fileName); + } + } catch (ComicNotFoundException e) { + e.printStackTrace(); + return false; + } catch (IOException e) { + e.printStackTrace(); + return false; + } catch (JSONException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + zip(files, zipName); + return true; + } + + /** + * Loads favorite list from zip to ComicReader folders + * + * @return + */ + public static boolean favLoad(String _zipFile) { + boolean successful = false; + + _dirChecker(getComicRoot() + "/props/"); + + successful = unzip(_zipFile); + + return successful; + } + + /** + * a method to help zip up fav files + * + * @param files + * which will hold all the files (path as well) that will be + * zipped up + * @param zipFile + * the name of the zip file (path as well) + */ + private static void zip(ArrayList files, String zipFile) { + try { + BufferedInputStream origin = null; + FileOutputStream dest = new FileOutputStream(zipFile); + + ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest)); + + byte data[] = new byte[BUFF_SIZE]; + + for (int i = 0; i < files.size(); i++) { + Log.v("Compress", "Adding: " + files.get(i)); + FileInputStream fi = new FileInputStream(files.get(i)); + origin = new BufferedInputStream(fi, BUFF_SIZE); + ZipEntry entry = new ZipEntry(files.get(i).substring(files.get(i).lastIndexOf("/") + 1)); + out.putNextEntry(entry); + int count; + while ((count = origin.read(data, 0, BUFF_SIZE)) != -1) { + out.write(data, 0, count); + } + origin.close(); + } + + out.close(); + } catch (Exception e) { + e.printStackTrace(); + } + + } + + /** + * a method to unzip up fav files + * + * @param files + * which will hold all the files (path as well) that will be + * zipped up + * @param zipFile + * the name of the zip file (path as well) + * @return true if successful + */ + public static boolean unzip(String _zipFile) { + try { + FileInputStream fin = new FileInputStream(_zipFile); + ZipInputStream zin = new ZipInputStream(fin); + ZipEntry ze = null; + while ((ze = zin.getNextEntry()) != null) { + Log.v("Decompress", "Unzipping " + ze.getName()); + String unzipLocation= ze.getName().equals("selected.json") ? getComicRoot() +"/"+ ze.getName() : getComicRoot() + "/props/"+ ze.getName(); + + FileOutputStream fout = new FileOutputStream(unzipLocation); + for (int c = zin.read(); c != -1; c = zin.read()) { + fout.write(c); + + } + zin.closeEntry(); + fout.close(); + + } + zin.close(); + } catch (Exception e) { + Log.e("Decompress", "unzip", e); + return false; + } + return true; + } + + /** + * adds directory if needed to comic reader. + * + * @param dir + */ + private static void _dirChecker(String dir) { + File f = new File(dir); + + if (!f.isDirectory()) { + f.mkdirs(); + } + } + }