/** * Parse a repository document. * * @param url * @throws IOException * @throws XmlPullParserException * @throws Exception */ void parseDocument(URL url) throws IOException, XmlPullParserException, Exception { if (!visited.contains(url)) { visited.add(url); try { System.out.println("Visiting: " + url); InputStream in = null; if (url.getPath().endsWith(".zip")) { ZipInputStream zin = new ZipInputStream(url.openStream()); ZipEntry entry = zin.getNextEntry(); while (entry != null) { if (entry.getName().equals("repository.xml")) { in = zin; break; } entry = zin.getNextEntry(); } } else { in = url.openStream(); } Reader reader = new InputStreamReader(in); XmlPullParser parser = new KXmlParser(); parser.setInput(reader); parseRepository(parser); } catch (MalformedURLException e) { System.out.println("Cannot create connection to url"); } } }
/** * Puts the uninstaller. * * @exception Exception Description of the Exception */ private void putUninstaller() throws Exception { // Me make the .uninstaller directory String dest = translatePath("$INSTALL_PATH") + File.separator + "Uninstaller"; String jar = dest + File.separator + "uninstaller.jar"; File pathMaker = new File(dest); pathMaker.mkdirs(); // We log the uninstaller deletion information UninstallData udata = UninstallData.getInstance(); udata.setUninstallerJarFilename(jar); udata.setUninstallerPath(dest); // We open our final jar file FileOutputStream out = new FileOutputStream(jar); ZipOutputStream outJar = new ZipOutputStream(out); idata.uninstallOutJar = outJar; outJar.setLevel(9); udata.addFile(jar); // We copy the uninstaller InputStream in = getClass().getResourceAsStream("/res/IzPack.uninstaller"); ZipInputStream inRes = new ZipInputStream(in); ZipEntry zentry = inRes.getNextEntry(); while (zentry != null) { // Puts a new entry outJar.putNextEntry(new ZipEntry(zentry.getName())); // Byte to byte copy int unc = inRes.read(); while (unc != -1) { outJar.write(unc); unc = inRes.read(); } // Next one please inRes.closeEntry(); outJar.closeEntry(); zentry = inRes.getNextEntry(); } inRes.close(); // We put the langpack in = getClass().getResourceAsStream("/langpacks/" + idata.localeISO3 + ".xml"); outJar.putNextEntry(new ZipEntry("langpack.xml")); int read = in.read(); while (read != -1) { outJar.write(read); read = in.read(); } outJar.closeEntry(); }
// Take a tree of files starting in a directory in a zip file // and copy them to a disk directory, recreating the tree. private int unpackZipFile( File inZipFile, String directory, String parent, boolean suppressFirstPathElement) { int count = 0; if (!inZipFile.exists()) return count; parent = parent.trim(); if (!parent.endsWith(File.separator)) parent += File.separator; if (!directory.endsWith(File.separator)) directory += File.separator; File outFile = null; try { ZipFile zipFile = new ZipFile(inZipFile); Enumeration zipEntries = zipFile.entries(); while (zipEntries.hasMoreElements()) { ZipEntry entry = (ZipEntry) zipEntries.nextElement(); String name = entry.getName().replace('/', File.separatorChar); if (name.startsWith(directory)) { if (suppressFirstPathElement) name = name.substring(directory.length()); outFile = new File(parent + name); // Create the directory, just in case if (name.indexOf(File.separatorChar) >= 0) { String p = name.substring(0, name.lastIndexOf(File.separatorChar) + 1); File dirFile = new File(parent + p); dirFile.mkdirs(); } if (!entry.isDirectory()) { System.out.println("Installing " + outFile); // Copy the file BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outFile)); BufferedInputStream in = new BufferedInputStream(zipFile.getInputStream(entry)); int size = 1024; int n = 0; byte[] b = new byte[size]; while ((n = in.read(b, 0, size)) != -1) out.write(b, 0, n); in.close(); out.flush(); out.close(); // Count the file count++; } } } zipFile.close(); } catch (Exception e) { System.err.println("...an error occured while installing " + outFile); e.printStackTrace(); System.err.println("Error copying " + outFile.getName() + "\n" + e.getMessage()); return -count; } System.out.println(count + " files were installed."); return count; }
/** * Zips byte array. * * @param input Input bytes. * @param initBufSize Initial buffer size. * @return Zipped byte array. * @throws IOException If failed. */ public static byte[] zipBytes(byte[] input, int initBufSize) throws IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(initBufSize); try (ZipOutputStream zos = new ZipOutputStream(bos)) { ZipEntry entry = new ZipEntry(""); try { entry.setSize(input.length); zos.putNextEntry(entry); zos.write(input); } finally { zos.closeEntry(); } } return bos.toByteArray(); }
/** * Unzips the transferred file. Returns <code>true</code> if the unzip was successful, and <code> * false</code> otherwise. In case of failure, this method handles setting an appropriate * response. */ private boolean completeUnzip(HttpServletRequest req, HttpServletResponse resp) throws ServletException { IPath destPath = new Path(getPath()); try { ZipFile source = new ZipFile(new File(getStorageDirectory(), FILE_DATA)); IFileStore destinationRoot = NewFileServlet.getFileStore(destPath); Enumeration<? extends ZipEntry> entries = source.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); IFileStore destination = destinationRoot.getChild(entry.getName()); if (entry.isDirectory()) destination.mkdir(EFS.NONE, null); else { destination.getParent().mkdir(EFS.NONE, null); IOUtilities.pipe( source.getInputStream(entry), destination.openOutputStream(EFS.NONE, null), false, true); } } source.close(); } catch (ZipException e) { // zip exception implies client sent us invalid input String msg = NLS.bind("Failed to complete file transfer on {0}", destPath.toString()); statusHandler.handleRequest( req, resp, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, msg, e)); return false; } catch (Exception e) { // other failures should be considered server errors String msg = NLS.bind("Failed to complete file transfer on {0}", destPath.toString()); statusHandler.handleRequest( req, resp, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg, e)); return false; } return true; }
public static void upgrade(File wrFile) throws Exception { System.out.println("Installing/upgrading Myna in '" + wrFile.toString() + "'..."); wrFile.mkdirs(); File web_inf = new File(wrFile.toURI().resolve("WEB-INF")); boolean isUpgrade = false; File backupDir = null; if (web_inf.exists()) { String dateString = new java.text.SimpleDateFormat("MM-dd-yyyy_HH.mm.ss.S").format(new Date()); String backupBase = "WEB-INF/upgrade_backups/backup_" + dateString; backupDir = new File(wrFile.toURI().resolve(backupBase)); backupDir.mkdirs(); isUpgrade = true; System.out.println("Backups stored in " + backupDir); // backup entire /myna folder because we're wiping it out FileUtils.copyDirectory( new File(wrFile.toURI().resolve("myna")), new File(backupDir.toURI().resolve("myna"))); FileUtils.deleteDirectory(new File(wrFile.toURI().resolve("myna"))); } if (isJar) { String jarFilePath = classUrl.substring(classUrl.indexOf(":") + 1, classUrl.indexOf("!")); File jarFile = new File(new java.net.URL(jarFilePath).toURI()); ZipFile zipFile = new ZipFile(jarFile); for (ZipEntry entry : java.util.Collections.list(zipFile.entries())) {; File outputFile = new File(wrFile.toURI().resolve(java.net.URLEncoder.encode(entry.getName(), "UTF-8"))); File backupFile = null; if (isUpgrade) { backupFile = new File( backupDir.toURI().resolve(java.net.URLEncoder.encode(entry.getName(), "UTF-8"))); } if (entry.isDirectory()) { outputFile.mkdirs(); if (isUpgrade) backupFile.mkdirs(); } else { if (isUpgrade && outputFile.exists()) { java.io.InputStream sourceIS = zipFile.getInputStream(entry); java.io.InputStream targetIS = FileUtils.openInputStream(outputFile); boolean isSame = IOUtils.contentEquals(sourceIS, targetIS); sourceIS.close(); targetIS.close(); if (isSame || entry.toString().equals("index.html") || entry.toString().equals("application.sjs") || entry.toString().equals("WEB-INF/classes/general.properties") || entry.toString().startsWith("WEB-INF/myna/ds")) { continue; } else { System.out.println("...backing up " + entry); FileUtils.copyFile(outputFile, backupFile, true); // outputFile.copyTo(backupFile); // fusebox.upgradeLog("Backup: " + backupFile); } } java.io.InputStream is = zipFile.getInputStream(entry); java.io.OutputStream os = FileUtils.openOutputStream(outputFile); IOUtils.copyLarge(is, os); is.close(); os.close(); } } zipFile.close(); // FileUtils.deleteDirectory() System.out.println("Done unpacking."); } }