List of usage examples for org.apache.commons.io IOUtils copyLarge
public static long copyLarge(Reader input, Writer output) throws IOException
Reader
to a Writer
. From source file:org.roda.core.util.FileUtility.java
public static Map<String, String> copyAndChecksums(InputStream in, OutputStream out, Collection<String> algorithms) throws NoSuchAlgorithmException, IOException { Map<String, String> ret = new HashMap<>(); Map<String, DigestInputStream> streams = new HashMap<>(); InputStream stream = in;/*www . ja va2s .c o m*/ for (String algorithm : algorithms) { stream = new DigestInputStream(stream, MessageDigest.getInstance(algorithm)); streams.put(algorithm, (DigestInputStream) stream); } IOUtils.copyLarge(stream, out); for (Entry<String, DigestInputStream> entry : streams.entrySet()) { ret.put(entry.getKey(), byteArrayToHexString(entry.getValue().getMessageDigest().digest())); } return ret; }
From source file:org.roda.core.util.ZipUtility.java
private static List<File> extractFilesFromInputStream(InputStream inputStream, File outputDir, boolean filesWithAbsolutePath) throws IOException { ZipInputStream zipInputStream = new ZipInputStream(inputStream); ZipEntry zipEntry = zipInputStream.getNextEntry(); if (zipEntry == null) { zipInputStream.close();/* ww w. jav a 2 s.co m*/ throw new IOException("No files inside ZIP"); } else { List<File> extractedFiles = new ArrayList<>(); while (zipEntry != null) { // for each entry to be extracted String entryName = zipEntry.getName(); LOGGER.debug("Extracting {}", entryName); File newFile = new File(outputDir, entryName); if (filesWithAbsolutePath) { extractedFiles.add(newFile); } else { extractedFiles.add(new File(entryName)); } if (zipEntry.isDirectory()) { newFile.mkdirs(); } else { if (newFile.getParentFile() != null && (!newFile.getParentFile().exists())) { newFile.getParentFile().mkdirs(); } FileOutputStream newFileOutputStream = new FileOutputStream(newFile); // copyLarge returns a long instead of int IOUtils.copyLarge(zipInputStream, newFileOutputStream); newFileOutputStream.close(); zipInputStream.closeEntry(); } zipEntry = zipInputStream.getNextEntry(); } zipInputStream.close(); return extractedFiles; } }
From source file:org.roda_project.commons_ip.model.impl.bagit.BagitSIP.java
private void createFiles(IPFile file, Path representationPath) { String relativeFilePath = ModelUtils.getFoldersFromList(file.getRelativeFolders()) + file.getFileName(); Path destination = representationPath.resolve(relativeFilePath); try {//from w w w . ja v a 2 s . c o m Files.createDirectories(destination.getParent()); try (InputStream input = Files.newInputStream(file.getPath()); OutputStream output = Files.newOutputStream(destination);) { IOUtils.copyLarge(input, output); } } catch (IOException e) { LOGGER.error("Error creating file {} on bagit data folder", file.getFileName(), e); } }
From source file:org.roda_project.commons_ip.model.impl.bagit.BagitSIP.java
private static SIP parseBagit(final Path source, final Path destinationDirectory) throws ParseException { IPConstants.METS_ENCODE_AND_DECODE_HREF = true; SIP sip = new BagitSIP(); BagFactory bagFactory = new BagFactory(); try (Bag bag = bagFactory.createBag(source.toFile())) { SimpleResult result = bag.verifyPayloadManifests(); if (result.isSuccess()) { Map<String, String> metadataMap = new HashMap<>(); for (NameValue nameValue : bag.getBagInfoTxt().asList()) { String key = nameValue.getKey(); String value = nameValue.getValue(); if (IPConstants.BAGIT_PARENT.equals(key)) { sip.setAncestors(Arrays.asList(value)); } else { if (IPConstants.BAGIT_ID.equals(key)) { sip.setId(value); }//w w w . j a v a 2 s .c om metadataMap.put(key, value); } } String vendor = metadataMap.get(IPConstants.BAGIT_VENDOR); Path metadataPath = destinationDirectory.resolve(Utils.generateRandomAndPrefixedUUID()); sip.addDescriptiveMetadata(BagitUtils.createBagitMetadata(metadataMap, metadataPath)); Map<String, IPRepresentation> representations = new HashMap<>(); for (BagFile bagFile : bag.getPayload()) { List<String> split = Arrays.asList(bagFile.getFilepath().split("/")); if (split.size() > 1 && IPConstants.BAGIT_DATA_FOLDER.equals(split.get(0))) { String representationId = "rep1"; int beginIndex = 1; if (IPConstants.BAGIT_VENDOR_COMMONS_IP.equals(vendor)) { representationId = split.get(1); beginIndex = 2; } if (!representations.containsKey(representationId)) { representations.put(representationId, new IPRepresentation(representationId)); } IPRepresentation representation = representations.get(representationId); List<String> directoryPath = split.subList(beginIndex, split.size() - 1); Path destPath = destinationDirectory.resolve(split.get(split.size() - 1)); try (InputStream bagStream = bagFile.newInputStream(); OutputStream destStream = Files.newOutputStream(destPath)) { IOUtils.copyLarge(bagStream, destStream); } IPFile file = new IPFile(destPath, directoryPath); representation.addFile(file); } } for (IPRepresentation rep : representations.values()) { sip.addRepresentation(rep); } } else { throw new ParseException(result.getMessages().toString()); } return sip; } catch (final IPException | IOException e) { throw new ParseException("Error parsing bagit SIP", e); } }
From source file:org.roda_project.commons_ip.utils.ZIPUtils.java
public static void unzip(Path zip, final Path dest) throws IOException { ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zip.toFile())); ZipEntry zipEntry = zipInputStream.getNextEntry(); if (zipEntry == null) { // No entries in ZIP zipInputStream.close();/*from ww w .ja v a 2 s. co m*/ } else { while (zipEntry != null) { // for each entry to be extracted String entryName = zipEntry.getName(); Path newFile = dest.resolve(entryName); if (zipEntry.isDirectory()) { Files.createDirectories(newFile); } else { if (!Files.exists(newFile.getParent())) { Files.createDirectories(newFile.getParent()); } OutputStream newFileOutputStream = Files.newOutputStream(newFile); IOUtils.copyLarge(zipInputStream, newFileOutputStream); newFileOutputStream.close(); zipInputStream.closeEntry(); } zipEntry = zipInputStream.getNextEntry(); } // end while zipInputStream.close(); } }
From source file:org.sakaiproject.nakamura.files.pool.StreamHelper.java
public void stream(HttpServletRequest request, ContentManager contentManager, Content node, String alternativeStream, HttpServletResponse response, Resource resource, ServletContext servletContext) throws IOException, StorageClientException, AccessDeniedException { InputStream dataStream = contentManager.getInputStream(node.getPath(), alternativeStream); if (dataStream == null) { response.sendError(HttpServletResponse.SC_NOT_FOUND); return;//w w w. ja v a 2 s. c o m } Map<String, Object> properties = node.getProperties(); long modifTime = StorageClientUtils.toLong( properties.get(StorageClientUtils.getAltField(Content.LASTMODIFIED_FIELD, alternativeStream))); if (unmodified(request, modifTime)) { response.setStatus(SC_NOT_MODIFIED); return; } setHeaders(properties, resource, response, alternativeStream, servletContext); setContentLength(properties, response, alternativeStream); IOUtils.copyLarge(dataStream, response.getOutputStream()); dataStream.close(); }
From source file:org.sakaiproject.nakamura.lite.content.FileStreamContentHelper.java
public Map<String, Object> writeBody(String keySpace, String columnFamily, String contentId, String contentBlockId, String streamId, Map<String, Object> content, InputStream in) throws IOException, StorageClientException { String path = getPath(keySpace, columnFamily, contentBlockId); File file = new File(fileStore + "/" + path); File parentFile = file.getParentFile(); if (!parentFile.exists()) { if (!parentFile.mkdirs()) { throw new IOException("Unable to create directory " + parentFile.getAbsolutePath()); }// w w w . j a va 2 s. c o m } FileOutputStream out = new FileOutputStream(file); long length = IOUtils.copyLarge(in, out); out.close(); LOGGER.debug("Wrote {} bytes to {} as body of {}:{}:{} stream {} ", new Object[] { length, path, keySpace, columnFamily, contentBlockId, streamId }); Map<String, Object> metadata = Maps.newHashMap(); metadata.put(StorageClientUtils.getAltField(Content.LENGTH_FIELD, streamId), length); metadata.put(StorageClientUtils.getAltField(Content.BLOCKID_FIELD, streamId), contentBlockId); metadata.put(StorageClientUtils.getAltField(STORE_LOCATION_FIELD, streamId), path); return metadata; }
From source file:org.sakaiproject.site.tool.SiteAction.java
/** * Handles uploading an archive file as part of the site creation workflow * @param data/*ww w . ja v a2 s .c o m*/ */ public void doUploadArchive(RunData data) { ParameterParser params = data.getParameters(); SessionState state = ((JetspeedRunData) data).getPortletSessionState(((JetspeedRunData) data).getJs_peid()); //get params FileItem fi = data.getParameters().getFileItem("importFile"); //get uploaded file into a location we can process String archiveUnzipBase = ServerConfigurationService.getString("archive.storage.path", FileUtils.getTempDirectoryPath()); //convert inputstream into actual file so we can unzip it String zipFilePath = archiveUnzipBase + File.separator + fi.getFileName(); //rudimentary check that the file is a zip file if (!StringUtils.endsWith(fi.getFileName(), ".zip")) { addAlert(state, rb.getString("archive.createsite.failedupload")); return; } File tempZipFile = new File(zipFilePath); if (tempZipFile.exists()) { tempZipFile.delete(); } FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream(tempZipFile); //copy contents into this file IOUtils.copyLarge(fi.getInputStream(), fileOutputStream); //set path into state so we can process it later state.setAttribute(STATE_UPLOADED_ARCHIVE_PATH, tempZipFile.getAbsolutePath()); state.setAttribute(STATE_UPLOADED_ARCHIVE_NAME, tempZipFile.getName()); } catch (Exception e) { M_log.error(e.getMessage(), e); //general catch all for the various exceptions that occur above. all are failures. addAlert(state, rb.getString("archive.createsite.failedupload")); } finally { IOUtils.closeQuietly(fileOutputStream); } //go to confirm screen state.setAttribute(STATE_TEMPLATE_INDEX, "10"); }
From source file:org.sonar.runner.impl.ServerConnection.java
/** * Download file, without any caching mechanism. * * @param urlPath path starting with slash, for instance {@code "/batch/index"} * @param toFile the target file//from w w w . ja va 2 s . c om * @throws IOException if connectivity problem or timeout (network) or IO error (when writing to file) * @throws IllegalStateException if HTTP response code is different than 2xx */ public void downloadFile(String urlPath, File toFile) throws IOException { if (!urlPath.startsWith("/")) { throw new IllegalArgumentException(format("URL path must start with slash: %s", urlPath)); } String url = baseUrlWithoutTrailingSlash + urlPath; logger.debug(format("Download %s to %s", url, toFile.getAbsolutePath())); ResponseBody responseBody = callUrl(url); try (OutputStream fileOutput = new FileOutputStream(toFile)) { IOUtils.copyLarge(responseBody.byteStream(), fileOutput); } catch (IOException | RuntimeException e) { FileUtils.deleteQuietly(toFile); throw e; } }
From source file:org.sourcepit.m2p2.cache.FileCacheTransport.java
private IStatus fromCache(OutputStream target, final IArtifactDescriptor descriptor, final File artifactFile) { InputStream in = null;// w w w. j a v a2 s .co m try { in = new FileInputStream(artifactFile); log.log(LogService.LOG_INFO, "Downloading " + descriptor.getArtifactKey().toExternalForm() + " (cached)"); IOUtils.copyLarge(new BufferedInputStream(in), target); return org.eclipse.core.runtime.Status.OK_STATUS; } catch (IOException e) { throw new IllegalStateException(e); } finally { IOUtils.closeQuietly(in); } }