/** * Actions-handling method. * * @param e The event. */ public void actionPerformed(ActionEvent e) { // Prepares the file chooser JFileChooser fc = new JFileChooser(); fc.setCurrentDirectory(new File(idata.getInstallPath())); fc.setMultiSelectionEnabled(false); fc.addChoosableFileFilter(fc.getAcceptAllFileFilter()); // fc.setCurrentDirectory(new File(".")); // Shows it try { if (fc.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) { // We handle the xml data writing File file = fc.getSelectedFile(); FileOutputStream out = new FileOutputStream(file); BufferedOutputStream outBuff = new BufferedOutputStream(out, 5120); parent.writeXMLTree(idata.xmlData, outBuff); outBuff.flush(); outBuff.close(); autoButton.setEnabled(false); } } catch (Exception err) { err.printStackTrace(); JOptionPane.showMessageDialog( this, err.toString(), parent.langpack.getString("installer.error"), JOptionPane.ERROR_MESSAGE); } }
public void write(File file, Drawing drawing) throws IOException { BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream(file)); try { write(out, drawing); } finally { if (out != null) { out.close(); } } }
// 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; }
public static int copyPlugin(JFrame frame, File source, String destFileName) { if (destFileName == null) destFileName = source.getName(); if (!PluginCore.userPluginDir.exists()) { boolean created = PluginCore.userPluginDir.mkdirs(); if (!created) { return UNABLE_TO_CREATE_DIR; } } File destFile = new File(PluginCore.userPluginDir, destFileName); BufferedInputStream in = null; BufferedOutputStream out = null; try { in = new BufferedInputStream(new FileInputStream(source)); out = new BufferedOutputStream(new FileOutputStream(destFile)); byte[] buf = new byte[1024]; int count; while ((count = in.read(buf, 0, buf.length)) > 0) { out.write(buf, 0, count); } } catch (IOException ex) { ex.printStackTrace(); return UNABLE_TO_COPY_FILE; } finally { if (in != null) { try { in.close(); } catch (IOException ignore) { // UNABLE_TO_COPY_FILE; } } if (out != null) { try { out.close(); } catch (IOException ignore) { // UNABLE_TO_COPY_FILE; } } } return SUCCESS; }
@Override public void run() { try { StringBuilder filePath = new StringBuilder(); filePath.append(Constants.IO.imageBaseDir).append(File.separator); filePath .append(card.hashCode()) .append(".") .append(card.getName().replace(":", "").replace("//", "-")) .append(".jpg"); File temporaryFile = new File(filePath.toString()); String imagePath = CardImageUtils.generateImagePath(card); TFile outputFile = new TFile(imagePath); if (!outputFile.exists()) { outputFile.getParentFile().mkdirs(); } File existingFile = new File(imagePath.replaceFirst("\\w{3}.zip", "")); if (existingFile.exists()) { new TFile(existingFile).cp_rp(outputFile); synchronized (sync) { update(cardIndex + 1, count); } existingFile.delete(); File parent = existingFile.getParentFile(); if (parent != null && parent.isDirectory() && parent.list().length == 0) { parent.delete(); } return; } BufferedOutputStream out; // Logger.getLogger(this.getClass()).info(url.toString()); URLConnection httpConn = url.openConnection(p); httpConn.connect(); if (((HttpURLConnection) httpConn).getResponseCode() == 200) { try (BufferedInputStream in = new BufferedInputStream(((HttpURLConnection) httpConn).getInputStream())) { // try (BufferedInputStream in = new // BufferedInputStream(url.openConnection(p).getInputStream())) { out = new BufferedOutputStream(new TFileOutputStream(temporaryFile)); byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) != -1) { // user cancelled if (cancel) { in.close(); out.flush(); out.close(); temporaryFile.delete(); return; } out.write(buf, 0, len); } } out.flush(); out.close(); if (card.isTwoFacedCard()) { BufferedImage image = ImageIO.read(temporaryFile); if (image.getHeight() == 470) { BufferedImage renderedImage = new BufferedImage(265, 370, BufferedImage.TYPE_INT_RGB); renderedImage.getGraphics(); Graphics2D graphics2D = renderedImage.createGraphics(); if (card.isTwoFacedCard() && card.isSecondSide()) { graphics2D.drawImage(image, 0, 0, 265, 370, 313, 62, 578, 432, null); } else { graphics2D.drawImage(image, 0, 0, 265, 370, 41, 62, 306, 432, null); } graphics2D.dispose(); writeImageToFile(renderedImage, outputFile); } else { new TFile(temporaryFile).cp_rp(outputFile); } temporaryFile.delete(); } else { new TFile(temporaryFile).cp_rp(outputFile); temporaryFile.delete(); } } else { Logger.getLogger(this.getClass()) .error(convertStreamToString(((HttpURLConnection) httpConn).getErrorStream())); } } catch (Exception e) { log.error(e, e); } synchronized (sync) { update(cardIndex + 1, count); } }