org.zeroturnaround.zip.commons.IOUtils Java Examples

The following examples show how to use org.zeroturnaround.zip.commons.IOUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Markdown.java    From zest-writer with GNU General Public License v3.0 6 votes vote down vote up
public String addHeaderAndFooterStrict(String content, String title) {

        try(InputStream is = MainApp.class.getResourceAsStream("assets/static/html/template-strict-begin.html"))  {
            String template= IOUtils.toString(is, "UTF-8");
            Matcher pathMatcher = Pattern.compile("%%(.*)%%").matcher(template);
            StringBuffer sb = new StringBuffer();
            while (pathMatcher.find()) {
                String path = MainApp.class.getResource("assets" + pathMatcher.group(1)).toExternalForm();
                pathMatcher.appendReplacement(sb, path);
            }
            pathMatcher.appendTail(sb);
            CONTENT_STRICT_BEFORE = new String(sb).replace("##title##", title.toUpperCase());
        } catch (IOException e) {
            log.error("Error when reading the template stream.", e);
        }

        return CONTENT_STRICT_BEFORE+content+CONTENT_STRICT_AFTER;
    }
 
Example #2
Source File: FileDownloadRequest.java    From selenium-grid-extensions with Apache License 2.0 6 votes vote down vote up
public File download(String pathToFile, String extension) {
    String encodedPath;
    encodedPath = Base64.getUrlEncoder().encodeToString(pathToFile.getBytes(StandardCharsets.UTF_8));

    HttpGet request = new HttpGet(String.format(FILE_DOWNLOAD_EXTENSION_PATH, sessionId, encodedPath));
    try {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse execute = httpClient.execute(httpHost, request);
        int statusCode = execute.getStatusLine().getStatusCode();
        if (HttpStatus.SC_OK == statusCode) {
            File downloadResult = File.createTempFile("download_result", extension);
            try (
                    FileOutputStream outputStream = new FileOutputStream(downloadResult);
                    InputStream responseStream = execute.getEntity().getContent()) {
                IOUtils.copy(responseStream, outputStream);
            }
            return downloadResult;
        } else {
            String message = IOUtils.toString(execute.getEntity().getContent(), "UTF-8");
            throw new FileDownloadException(message, statusCode);
        }
    } catch (IOException e) {
        throw new FileDownloadException(e);
    }
}
 
Example #3
Source File: FileDownloadRequest.java    From selenium-grid-extensions with Apache License 2.0 6 votes vote down vote up
public File download(String pathToFile) {
    String encodedPath;
    encodedPath = Base64.getUrlEncoder().encodeToString(pathToFile.getBytes(StandardCharsets.UTF_8));

    HttpGet request = new HttpGet(String.format(FILE_DOWNLOAD_EXTENSION_PATH, sessionId, encodedPath));
    try {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse execute = httpClient.execute(httpHost, request);
        int statusCode = execute.getStatusLine().getStatusCode();
        if (HttpStatus.SC_OK == statusCode) {
            File downloadResult = File.createTempFile("download_result", ".tmp");
            try (
                    FileOutputStream outputStream = new FileOutputStream(downloadResult);
                    InputStream responseStream = execute.getEntity().getContent()) {
                IOUtils.copy(responseStream, outputStream);
            }
            return downloadResult;
        } else {
            String message = IOUtils.toString(execute.getEntity().getContent(), "UTF-8");
            throw new FileDownloadException(message, statusCode);
        }
    } catch (IOException e) {
        throw new FileDownloadException(e);
    }
}
 
Example #4
Source File: MdCheatSheetDialog.java    From zest-writer with GNU General Public License v3.0 5 votes vote down vote up
@FXML private void initialize() {
    List<String> chaptersTitles = new ArrayList<>();

    String cheatSheet = "";
    try {
        cheatSheet = IOUtils.toString(MainApp.class.getResourceAsStream(CHEAT_SHEET_LOCATION), "UTF-8");
    } catch (IOException e) {
        logger.error("Error when reading the cheatSheet stream.", e);
    }

    Matcher titleMatcher = Pattern.compile(TITLE_REGEX).matcher(cheatSheet);
    while (titleMatcher.find()) {
        chaptersTitles.add(Configuration.getBundle().getString("ui.md_cheat_sheet." + titleMatcher.group(1)));
    }

    String[] chaptersContents = cheatSheet.split(TITLE_REGEX);
    List<Tab> tabs = cheatSheetTabPane.getTabs();

    for (int i=1 ; i<chaptersContents.length; i++) {
        Tab tab = new Tab();

        tab.setText(chaptersTitles.get(i-1));

        WebView webView = new WebView();
        String chapterContent = MainApp.getMdUtils().addHeaderAndFooter(chaptersContents[i]);

        webView.getEngine().loadContent(chapterContent);

        tab.setContent(webView);
        tabs.add(tab);
    }
}
 
Example #5
Source File: DiffDisplayDialog.java    From zest-writer with GNU General Public License v3.0 4 votes vote down vote up
public DiffDisplayDialog(File file, String newContent, String titleContent, String titleExtract) {
	super(Configuration.getBundle().getString("ui.dialog.download.compare.window.title"), Configuration.getBundle().getString("ui.dialog.download.compare.window.header"));
	this.file = file;
	this.newContent = newContent;
	this.titleContent = titleContent;
       this.titleExtract = titleExtract;
       try {
           if(this.file.exists()) {
               this.oldContent = IOUtils.toString(new FileInputStream(this.file), "UTF-8");
           }
       } catch (IOException e) {
           log.error(e.getMessage(), e);
       }
       ;

    // Set the button types.
    ButtonType validButtonType = new ButtonType(Configuration.getBundle().getString("ui.dialog.download.compare.button.confirm"), ButtonData.OK_DONE);
    this.getDialogPane().getButtonTypes().addAll(validButtonType, ButtonType.CANCEL);

	// Create the username and password labels and fields.
	GridPane grid = new GridPane();
	grid.setHgap(10);
	grid.setVgap(10);
	grid.setPadding(new Insets(20, 20, 10, 10));
       Label l01 = new Label(Configuration.getBundle().getString("ui.dialog.download.compare.content_name") + " : "+titleContent);
       Label l02 = new Label(Configuration.getBundle().getString("ui.dialog.download.compare.extract_name") + " : "+titleExtract);
	Label l1 = new Label(Configuration.getBundle().getString("ui.dialog.download.compare.old_content"));
       Label l2 = new Label(Configuration.getBundle().getString("ui.dialog.download.compare.new_content"));
	TextArea textArea1 = new TextArea();
	textArea1.setEditable(false);
       textArea1.setWrapText(true);
       textArea1.setPrefHeight(500);
       textArea1.setText(oldContent);
       ScrollPane scrollPane1 = new ScrollPane();
       scrollPane1.setContent(textArea1);
       scrollPane1.setFitToWidth(true);
	TextArea textArea2 = new TextArea();
       textArea2.setEditable(false);
       textArea2.setWrapText(true);
       textArea2.setPrefHeight(500);
       textArea2.setText(newContent);
       ScrollPane scrollPane2 = new ScrollPane();
       scrollPane2.setContent(textArea2);
       scrollPane2.setFitToWidth(true);


       grid.add(l01, 0, 0,2, 1);
       grid.add(l02, 0, 1, 2, 1);
	grid.add(l1, 0, 2);
    grid.add(l2, 1, 2);
       grid.add(scrollPane1, 0, 3);
       grid.add(scrollPane2, 1, 3);

    // Enable/Disable login button depending on whether a username was entered.
	this.getDialogPane().lookupButton(validButtonType);

	this.getDialogPane().setContent(grid);

	Platform.runLater(textArea1::requestFocus);

	this.setResultConverter(dialogButton -> {
		if(dialogButton == validButtonType) {
               return true;
		}
		return false;
	});
}
 
Example #6
Source File: DownloadContentService.java    From zest-writer with GNU General Public License v3.0 4 votes vote down vote up
private Map<File, String> inputToMapping(InputStream src, File dest) throws IOException {
    Map<File, String> mapping = new HashMap<>();
    String text = IOUtils.toString(src, "UTF-8");
    mapping.put(dest, text);
    return mapping;
}
 
Example #7
Source File: ResourceUploadRequest.java    From selenium-grid-extensions with Apache License 2.0 4 votes vote down vote up
private String contentAsString(CloseableHttpResponse execute) throws IOException {
    InputStream response = execute.getEntity().getContent();
    return IOUtils.toString(response, "utf-8");
}
 
Example #8
Source File: Packr.java    From packr with Apache License 2.0 4 votes vote down vote up
private void copyAndMinimizeJRE(PackrOutput output, PackrConfig config) throws IOException {

		boolean extractToCache = config.cacheJre != null;
		boolean skipExtractToCache = false;

		// check if JRE extraction (and minimize) can be skipped
		if (extractToCache && config.cacheJre.exists()) {
			if (config.cacheJre.isDirectory()) {
				// check if the cache directory is empty
				String[] files = config.cacheJre.list();
				skipExtractToCache = files != null && files.length > 0;
			} else {
				throw new IOException(config.cacheJre + " must be a directory");
			}
		}

		// path to extract JRE to (cache, or target folder)
		File jreStoragePath = extractToCache ? config.cacheJre : output.resourcesFolder;

		if (skipExtractToCache) {
			System.out.println("Using cached JRE in '" + config.cacheJre + "' ...");
		} else {
			// path to extract JRE from (folder, zip or remote)
			boolean fetchFromRemote = config.jdk.startsWith("http://") || config.jdk.startsWith("https://");
			File jdkFile = fetchFromRemote ? new File(jreStoragePath, "jdk.zip") : new File(config.jdk);

			// download from remote
			if (fetchFromRemote) {
				System.out.println("Downloading JDK from '" + config.jdk + "' ...");
				try (InputStream remote = new URL(config.jdk).openStream()) {
					try (OutputStream outJdk = new FileOutputStream(jdkFile)) {
						IOUtils.copy(remote, outJdk);
					}
				}
			}

			// unpack JDK zip (or copy if it's a folder)
			System.out.println("Unpacking JRE ...");
			File tmp = new File(jreStoragePath, "tmp");
			if (tmp.exists()) {
				PackrFileUtils.deleteDirectory(tmp);
			}
			PackrFileUtils.mkdirs(tmp);

			if (jdkFile.isDirectory()) {
				PackrFileUtils.copyDirectory(jdkFile, tmp);
			} else {
				ZipUtil.unpack(jdkFile, tmp);
			}

			// copy the JRE sub folder
			File jre = searchJre(tmp);
			if (jre == null) {
				throw new IOException("Couldn't find JRE in JDK, see '" + tmp.getAbsolutePath() + "'");
			}

			PackrFileUtils.copyDirectory(jre, new File(jreStoragePath, "jre"));
			PackrFileUtils.deleteDirectory(tmp);

			if (fetchFromRemote) {
				PackrFileUtils.delete(jdkFile);
			}

			// run minimize
			PackrReduce.minimizeJre(jreStoragePath, config);
		}

		if (extractToCache) {
			// if cache is used, copy again here; if the JRE is cached already,
			// this is the only copy done (and everything above is skipped)
			PackrFileUtils.copyDirectory(jreStoragePath, output.resourcesFolder);
		}
	}
 
Example #9
Source File: Packr.java    From packr with Apache License 2.0 4 votes vote down vote up
private byte[] readResource(String resource) throws IOException {
	return IOUtils.toByteArray(Packr.class.getResourceAsStream(resource));
}
 
Example #10
Source File: Packr.java    From packr with Apache License 2.0 4 votes vote down vote up
private String readResourceAsString(String resource, Map<String, String> values) throws IOException {
	String txt = IOUtils.toString(Packr.class.getResourceAsStream(resource), "UTF-8");
	return replace(txt, values);
}