private boolean doesCrcMatch(int plugin) { // Log.d("Angband","doesCrcMatch "+plugin); boolean result = false; try { File f = new File(Preferences.getAngbandFilesDirectory(plugin)); f.mkdirs(); String filename = f.getAbsolutePath() + "/crc" + Plugins.getFilesDir(Plugins.Plugin.convert(plugin)); File myfile = new File(filename); FileInputStream fis = new FileInputStream(myfile); String currentCrc = new Scanner(fis).useDelimiter("\\A").next().trim(); // Log.d("Angband","doesCrcMatch.currentcrc="+currentCrc); // Log.d("Angband","doesCrcMatch.plugincrc="+Plugins.getPluginCrc(plugin)); result = (Plugins.getPluginCrc(plugin).compareTo(currentCrc) == 0); } catch (Exception e) { Log.v("Angband", "doesCrcMatch.error reading crc: " + e); } return result; }
private boolean extractPluginResources(int plugin) { // Log.d("Angband","extractPluginResources "+plugin); boolean result = true; try { File f = new File(Preferences.getAngbandFilesDirectory(plugin)); f.mkdirs(); String abs_path = f.getAbsolutePath(); // Log.v("Angband", "installing to " + abs_path); // copy game files ZipInputStream zis = Plugins.getPluginZip(plugin); ZipEntry ze; while ((ze = zis.getNextEntry()) != null) { String ze_name = ze.getName(); // Log.v("Angband", "extracting " + ze_name); String filename = abs_path + "/" + ze_name; File myfile = new File(filename); if (ze.isDirectory()) { myfile.mkdirs(); continue; } byte contents[] = new byte[(int) ze.getSize()]; FileOutputStream fos = new FileOutputStream(myfile); int remaining = (int) ze.getSize(); int totalRead = 0; while (remaining > 0) { int readlen = zis.read(contents, 0, remaining); fos.write(contents, 0, readlen); totalRead += readlen; remaining -= readlen; } fos.close(); // perform a basic length validation myfile = new File(filename); if (myfile.length() != totalRead) { // Log.v("Angband", "Installer.length mismatch: " + filename); message = "Error: failed to verify installed file on sdcard: " + filename; throw new IllegalStateException(); } zis.closeEntry(); } zis.close(); // update crc file File myfile_crc = new File(abs_path + "/crc" + Plugins.getFilesDir(Plugins.Plugin.convert(plugin))); if (myfile_crc.exists()) myfile_crc.delete(); Writer crc_out = new OutputStreamWriter(new FileOutputStream(myfile_crc)); String crc_val = Plugins.getPluginCrc(plugin); crc_out.write(crc_val); crc_out.close(); } catch (Exception e) { result = false; if (message.length() == 0) message = "Error: failed to install files to sdcard. " + e.getMessage(); // Log.v("Angband", "error extracting files: " + e); } return result; }