private String unzipSharedDeckFile(String zipFilename, String title) {
    ZipInputStream zipInputStream = null;
    Log.i(AnkiDroidApp.TAG, "unzipSharedDeckFile");
    if (zipFilename.endsWith(".zip")) {
      Log.i(AnkiDroidApp.TAG, "zipFilename ends with .zip");
      try {
        zipInputStream = new ZipInputStream(new FileInputStream(new File(zipFilename)));

        title = title.replace("^", "");
        title = title.substring(0, java.lang.Math.min(title.length(), 40));

        if (new File(mDestination + "/" + title + ".anki").exists()) {
          title += System.currentTimeMillis();
        }

        String partialDeckPath = mDestination + "/tmp/" + title;
        String deckFilename = partialDeckPath + ".anki.updating";

        ZipEntry zipEntry = null;
        while ((zipEntry = zipInputStream.getNextEntry()) != null) {
          Log.i(AnkiDroidApp.TAG, "zipEntry = " + zipEntry.getName());

          if ("shared.anki".equalsIgnoreCase(zipEntry.getName())) {
            Utils.writeToFile(zipInputStream, deckFilename);
          } else if (zipEntry.getName().startsWith("shared.media/", 0)) {
            Log.i(
                AnkiDroidApp.TAG,
                "Folder created = " + new File(partialDeckPath + ".media/").mkdir());
            Log.i(
                AnkiDroidApp.TAG,
                "Destination = "
                    + AnkiDroidApp.getStorageDirectory()
                    + "/"
                    + title
                    + ".media/"
                    + zipEntry.getName().replace("shared.media/", ""));
            Utils.writeToFile(
                zipInputStream,
                partialDeckPath + ".media/" + zipEntry.getName().replace("shared.media/", ""));
          }
        }
        zipInputStream.close();

        // Delete zip file
        new File(zipFilename).delete();
      } catch (FileNotFoundException e) {
        Log.e(AnkiDroidApp.TAG, "FileNotFoundException = " + e.getMessage());
        e.printStackTrace();
      } catch (IOException e) {
        Log.e(AnkiDroidApp.TAG, "IOException = " + e.getMessage());
        e.printStackTrace();
      }
    }

    return title;
  }
Exemplo n.º 2
0
 private static File getBackupDirectory() {
   SharedPreferences prefs =
       AnkiDroidApp.getSharedPrefs(AnkiDroidApp.getInstance().getBaseContext());
   File directory =
       new File(prefs.getString("deckPath", AnkiDroidApp.getStorageDirectory()) + BACKUP_SUFFIX);
   if (!directory.isDirectory()) {
     directory.mkdirs();
   }
   return directory;
 }
 private void restorePreferences() {
   SharedPreferences pref = PrefSettings.getSharedPrefs(getBaseContext());
   mUsername = pref.getString("username", "");
   mPassword = pref.getString("password", "");
   mDestination = pref.getString("deckPath", AnkiDroidApp.getStorageDirectory());
 }