List of usage examples for org.apache.commons.compress.archivers.tar TarArchiveInputStream getNextTarEntry
public TarArchiveEntry getNextTarEntry() throws IOException
From source file:org.pentaho.webpackage.deployer.archive.impl.UrlTransformer.java
boolean canHandleTarGzFile(File file) { TarArchiveInputStream tarInput = null; try {/* www . ja v a 2s . c o m*/ tarInput = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(file))); TarArchiveEntry currentEntry = tarInput.getNextTarEntry(); while (currentEntry != null) { if (currentEntry.getName().endsWith(WebPackageURLConnection.PACKAGE_JSON)) { return true; } currentEntry = tarInput.getNextTarEntry(); } } catch (IOException ignored) { // Ignore } finally { if (tarInput != null) { try { tarInput.close(); } catch (IOException ignored) { // Ignore } } } return false; }
From source file:org.pentaho.webpackage.deployer.UrlTransformer.java
@Override public boolean canHandle(File file) { if (!file.exists()) { return false; }//from w w w. j a v a 2 s. c o m if (file.getName().endsWith(".tgz")) { TarArchiveInputStream tarInput = null; try { tarInput = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(file))); TarArchiveEntry currentEntry = tarInput.getNextTarEntry(); while (currentEntry != null) { if (currentEntry.getName().endsWith("package.json")) { return true; } currentEntry = tarInput.getNextTarEntry(); } } catch (IOException e) { e.printStackTrace(); } finally { if (tarInput != null) { try { tarInput.close(); } catch (IOException ignored) { // Ignore } } } return false; } else if (file.getName().endsWith(".zip") || file.getName().endsWith(".jar")) { ZipFile zipFile = null; try { zipFile = new ZipFile(file); return zipFile.getEntry("package.json") != null && zipFile.getEntry("META-INF/MANIFEST.MF") == null; } catch (IOException e) { this.logger.error(e.getMessage(), e); } finally { if (zipFile != null) { try { zipFile.close(); } catch (IOException ignored) { // Ignore } } } } return false; }
From source file:org.queeg.hadoop.tar.TarExtractor.java
public void extract(ByteSource source) throws IOException { TarArchiveInputStream archiveInputStream = new TarArchiveInputStream(source.openStream()); TarArchiveEntry entry;/*w w w . ja v a 2 s . co m*/ while ((entry = archiveInputStream.getNextTarEntry()) != null) { if (entry.isFile()) { BoundedInputStream entryInputStream = new BoundedInputStream(archiveInputStream, entry.getSize()); ByteSink sink = new PathByteSink(conf, new Path(destination, entry.getName())); sink.writeFrom(entryInputStream); } else if (entry.isDirectory()) { ByteStreams.skipFully(archiveInputStream, entry.getSize()); fs.mkdirs(new Path(destination, entry.getName())); } } archiveInputStream.close(); }
From source file:org.renjin.cran.ProjectBuilder.java
private void unpackSources(File sourceArchive) throws IOException { FileInputStream in = new FileInputStream(sourceArchive); GZIPInputStream gzipIn = new GZIPInputStream(in); TarArchiveInputStream tarIn = new TarArchiveInputStream(gzipIn); TarArchiveEntry entry;//from w w w .ja va 2 s .co m while ((entry = tarIn.getNextTarEntry()) != null) { if (entry.getName().endsWith(".Rd")) { } else if (entry.getName().startsWith(pkg + "/src/") && entry.getSize() != 0) { } else if (entry.getName().startsWith(pkg + "/R/") && entry.getSize() != 0) { extractTo(entry, tarIn, rSourcesDir); } else if (entry.getName().equals(pkg + "/DESCRIPTION")) { extractTo(entry, tarIn, baseDir); } else if (entry.getName().equals(pkg + "/NAMESPACE")) { extractTo(entry, tarIn, baseDir); } else if (entry.getName().startsWith(pkg + "/tests/") && entry.getSize() != 0) { extractTo(entry, tarIn, rTestsDir); } else if (entry.getName().startsWith(pkg + "/data/") && entry.getSize() != 0) { extractTo(entry, tarIn, resourcesDir); addDataset(entry); } } }
From source file:org.robovm.compilerhelper.Archiver.java
public static void unarchive(File archiveFile, File destinationDirectory) throws IOException { TarArchiveInputStream tar = new TarArchiveInputStream( new GzipCompressorInputStream(new BufferedInputStream(new FileInputStream(archiveFile)))); destinationDirectory.mkdirs();/*from w ww . j a v a 2 s . c o m*/ TarArchiveEntry entry = tar.getNextTarEntry(); while (entry != null) { File f = new File(destinationDirectory, entry.getName()); if (entry.isDirectory()) { f.mkdirs(); entry = tar.getNextTarEntry(); continue; } // TODO make this a bit cleaner String parentDir = f.getPath(); if (parentDir != null) { new File(parentDir.substring(0, parentDir.lastIndexOf(File.separator))).mkdirs(); } f.createNewFile(); byte[] bytes = new byte[1024]; int count; BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f)); while ((count = tar.read(bytes)) > 0) { out.write(bytes, 0, count); } out.flush(); out.close(); entry = tar.getNextTarEntry(); } }
From source file:org.rsna.ctp.stdstages.ArchiveImportService.java
private void expandTAR(File tar, File dir) { try {// www. j ava 2 s.com TarArchiveInputStream tais = new TarArchiveInputStream(new FileInputStream(tar)); TarArchiveEntry tae; while ((tae = tais.getNextTarEntry()) != null) { if (!tae.isDirectory()) { FileOutputStream fos = new FileOutputStream(new File(dir, tae.getName())); byte[] buf = new byte[4096]; long count = tae.getSize(); while (count > 0) { int n = tais.read(buf, 0, buf.length); fos.write(buf, 0, n); count -= n; } fos.flush(); fos.close(); } } tais.close(); } catch (Exception ex) { logger.warn("Unable to expand: \"" + tar + "\"", ex); } }
From source file:org.sakaiproject.vtlgen.api.PackageUtil.java
public static void untar(String fileName, String targetPath) throws IOException { File tarArchiveFile = new File(fileName); BufferedOutputStream dest = null; FileInputStream tarArchiveStream = new FileInputStream(tarArchiveFile); TarArchiveInputStream tis = new TarArchiveInputStream(new BufferedInputStream(tarArchiveStream)); TarArchiveEntry entry = null;/*from w w w.ja v a 2s.c om*/ try { while ((entry = tis.getNextTarEntry()) != null) { int count; File outputFile = new File(targetPath, entry.getName()); if (entry.isDirectory()) { // entry is a directory if (!outputFile.exists()) { outputFile.mkdirs(); } } else { // entry is a file byte[] data = new byte[BUFFER_MAX]; FileOutputStream fos = new FileOutputStream(outputFile); dest = new BufferedOutputStream(fos, BUFFER_MAX); while ((count = tis.read(data, 0, BUFFER_MAX)) != -1) { dest.write(data, 0, count); } dest.flush(); dest.close(); } } } catch (Exception e) { e.printStackTrace(); } finally { if (dest != null) { dest.flush(); dest.close(); } tis.close(); } }
From source file:org.sead.sda.LandingPage.java
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (request.getParameter("tag") != null || request.getRequestURI().contains("/sda/list")) { String tag = ""; if (request.getParameter("tag") != null) { tag = request.getParameter("tag"); } else {//from www . j av a 2 s .c o m tag = request.getRequestURI().split("/sda/list=")[1]; } // here we check whether the BagIt zip file for this RO exists in SDA SFTP sftp = new SFTP(); String bagName = getBagNameFromId(tag); if (sftp.doesFileExist(Constants.sdaPath + bagName + "/" + bagName + ".zip")) { System.out.println("Bag Exists in SDA..."); request.setAttribute("bagExists", "true"); } sftp.disConnectSessionAndChannel(); request.setAttribute("obTag", tag); request.setAttribute("landingPageUrl", Constants.landingPage); String keyList_cp = "@id|status|message|preferences"; String keyList_ore = "keyword|contact|creator|publication date|title|abstract|license|is version of|similarto|title|describes|@context|aggregates|has part|identifier|label|size"; // keyMapList = new HashMap<String, String>(); Shimcalls shim = new Shimcalls(); // Fix: accessing RO from c3pr here is wrong. we have to access the ore map in the // published package and read properties from that. JSONObject cp = shim.getResearchObject(tag); if (cp.isEmpty()) { RequestDispatcher dispatcher = request.getRequestDispatcher("/ro.jsp"); request.setAttribute("roExists", "false"); dispatcher.forward(request, response); return; } request.setAttribute("roExists", "true"); SeadMon.addLog(MonConstants.Components.LANDING_PAGE, tag, MonConstants.EventType.ACCESS); keyMap(cp, keyList_cp); shim.getObjectID(cp, "@id"); String oreUrl = shim.getID(); JSONObject oreFile = shim.getResearchObjectORE(oreUrl); keyMap(oreFile, keyList_ore); JSONObject describes = (JSONObject) oreFile.get(keyMapList.get("describes")); Map<String, List<String>> roProperties = new HashMap<String, List<String>>(); Map<String, String> downloadList = new HashMap<String, String>(); Map<String, String> linkedHashMap = new LinkedHashMap<String, String>(); Map<String, String> linkedHashMapTemp = new LinkedHashMap<String, String>(); Map<String, String> newDownloadList = new LinkedHashMap<String, String>(); // extract properties from ORE JSONArray status = (JSONArray) cp.get(keyMapList.get("Status".toLowerCase())); String doi = "No DOI Found"; // handle this as an exception String pubDate = null; for (Object st : status) { JSONObject jsonStatus = (JSONObject) st; String stage = (String) jsonStatus.get("stage"); if ("Success".equals(stage)) { doi = (String) jsonStatus.get("message"); pubDate = (String) jsonStatus.get("date"); } } roProperties.put("DOI", Arrays.asList(doi)); roProperties.put("Publication Date", Arrays.asList(pubDate)); roProperties.put("Full Metadata", Arrays.asList(Constants.landingPage + "/metadata/" + tag + "/oremap")); addROProperty("Creator", describes, roProperties); // addROProperty("Publication Date", describes, roProperties); addROProperty("Title", describes, roProperties); addROProperty("Abstract", describes, roProperties); addROProperty("Contact", describes, roProperties); addROProperty("Keyword", describes, roProperties); JSONObject preferences = (JSONObject) cp.get(keyMapList.get("Preferences".toLowerCase())); //addROProperty_License("License", preferences, cp, roProperties); addROProperty("License", preferences, roProperties); // check access rights if (isRORestricted(preferences)) { request.setAttribute("accessRestricted", "true"); List<String> rights = new ArrayList<String>(); rights.add("Restricted"); roProperties.put("Access Rights", rights); } //Map<String, String> properties = new HashMap<String, String>(); //String Label = properties.get("Label"); // extract Live Data Links from ORE String liveCopy = null; if (describes.get(keyMapList.get("Is Version Of".toLowerCase())) != null) { String versionOf = describes.get(keyMapList.get("Is Version Of".toLowerCase())).toString(); if (versionOf.startsWith("http")) { liveCopy = versionOf; } else if (describes.get(keyMapList.get("similarTo".toLowerCase())) != null) { String similar = describes.get(keyMapList.get("similarTo".toLowerCase())).toString(); similar = similar.substring(0, similar.indexOf("/resteasy") + 1); liveCopy = similar + "#collection?uri=" + versionOf; } } if (liveCopy != null) { List<String> liveCopyList = new ArrayList<String>(); if (shim.validUrl(liveCopy)) { liveCopyList.add(liveCopy); } else { liveCopyList.add("Not Available"); } roProperties.put("Live Data Links", liveCopyList); } // set properties as an attribute request.setAttribute("roProperties", roProperties); // String title = describes.get(keyMapList.get("Title".toLowerCase())).toString(); // extract file names from tar archive in SDA String requestURI = request.getRequestURI(); if (requestURI.contains("/sda/list")) { int c = 0; String[] requestURIsda = requestURI.split("/"); for (String item : requestURIsda) { if (item.equals("sda")) { c++; } } if (c % 2 != 0) { //extract RO hierarchy try { NewOREmap oreMap = new NewOREmap(oreFile, keyMapList); downloadList = oreMap.getHierarchy(); Set<String> nameList = downloadList.keySet(); for (String name : nameList) { String[] name_split = name.split("/"); String size = null; if (downloadList.get(name) != null) { int bytes = Integer.parseInt(downloadList.get(name)); int kb = bytes / 1024; int mb = kb / 1024; int gb = mb / 1024; if (bytes <= 1024) { size = bytes + " Bytes"; } else if (kb <= 1024) { size = kb + " KB"; } else if (mb <= 1024) { size = mb + " MB"; } else { size = gb + " GB"; } } String temp = null; if (name_split.length <= 2 && size != null) { temp = "<span style='padding-left:" + 30 * (name_split.length - 2) + "px'>" + name_split[name_split.length - 1] + "</span>"; linkedHashMap.put(name, temp); } else { temp = "<span style='padding-left:" + 30 * (name_split.length - 2) + "px'>" + "|__" + name_split[name_split.length - 1] + "</span>"; linkedHashMapTemp.put(name, temp); } newDownloadList.put(name, size); } for (String key : linkedHashMapTemp.keySet()) { linkedHashMap.put(key, linkedHashMapTemp.get(key)); } } catch (Exception e) { System.err.println("Landing Page OREmap error: inaccurate keys"); } } // set download list as an attribute // set linkedHashMap as an attribute } request.setAttribute("downloadList", newDownloadList); request.setAttribute("linkedHashMap", linkedHashMap); // forward the user to get_id UI RequestDispatcher dispatcher = request.getRequestDispatcher("/ro.jsp"); dispatcher.forward(request, response); } else if (!request.getRequestURI().contains("bootstrap")) { // collection title is the last part of the request URI String requestURI = request.getRequestURI(); String newURL = requestURI.substring(requestURI.lastIndexOf("sda/") + 4); String title = null; String filename = null; if (!newURL.contains("/")) { title = newURL; } else { title = newURL.split("/")[0]; filename = newURL.substring(newURL.indexOf("/") + 1); } title = URLDecoder.decode(title, "UTF-8"); newURL = URLDecoder.decode(newURL, "UTF-8"); // don't allow downloads for restricted ROs // Fix: use ORE from package Shimcalls shim = new Shimcalls(); JSONObject ro = shim.getResearchObject(title); String keyList_cp = "@id|status|message|preferences"; keyMapList = new HashMap<String, String>(); keyMap(ro, keyList_cp); if (isRORestricted((JSONObject) ro.get(keyMapList.get("Preferences".toLowerCase())))) { return; } SFTP sftp = new SFTP(); String bgName = getBagNameFromId(title); String target = Constants.sdaPath + bgName + "/" + bgName + ".zip"; if (!sftp.doesFileExist(target)) { target = Constants.sdaPath + title + "/" + title + ".tar"; } System.out.println("title " + title); System.out.println("filename " + filename); if (!title.equals("*")) { InputStream inStream = sftp.downloadFile(target); String mimeType = "application/octet-stream"; response.setContentType(mimeType); String headerKey = "Content-Disposition"; String headerValue = null; if (filename != null) { if (filename.contains("/")) { filename = filename.substring(filename.lastIndexOf("/") + 1); } headerValue = String.format("attachment; filename=\"%s\"", filename); } else { headerValue = String.format("attachment; filename=\"%s\"", target.substring(target.lastIndexOf("/") + 1)); } response.setHeader(headerKey, headerValue); OutputStream outStream = response.getOutputStream(); if (newURL.equals(title)) { //download tar file SeadMon.addLog(MonConstants.Components.LANDING_PAGE, title, MonConstants.EventType.DOWNLOAD); System.out.println("SDA download path: " + target); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, bytesRead); } } else { //download individual files if (target.contains(".tar")) { System.out.println("SDA download path: " + Constants.sdaPath + newURL); TarArchiveInputStream myTarFile = new TarArchiveInputStream(inStream); TarArchiveEntry entry = null; String individualFiles; int offset; while ((entry = myTarFile.getNextTarEntry()) != null) { individualFiles = entry.getName(); if (individualFiles.equals(newURL)) { byte[] content = new byte[(int) entry.getSize()]; offset = 0; myTarFile.read(content, offset, content.length - offset); outStream.write(content); } } myTarFile.close(); } else { System.out.println("SDA download path: " + Constants.sdaPath + bgName + "/" + bgName + ".zip/" + bgName + "/" + newURL.substring(newURL.indexOf("/") + 1)); BufferedInputStream bin = new BufferedInputStream(inStream); ZipInputStream myZipFile = new ZipInputStream(bin); ZipEntry ze = null; while ((ze = myZipFile.getNextEntry()) != null) { if (ze.getName().equals(bgName + "/" + newURL.substring(newURL.indexOf("/") + 1))) { byte[] buffer = new byte[4096]; int len; while ((len = myZipFile.read(buffer)) != -1) { outStream.write(buffer, 0, len); } break; } } } } inStream.close(); outStream.close(); } sftp.disConnectSessionAndChannel(); } }
From source file:org.slc.sli.bulk.extract.files.ExtractFileTest.java
/** * Test generation of archive file.//from w w w . j a v a2s . c o m * @throws Exception */ @Test public void generateArchiveTest() throws Exception { String fileName = archiveFile.getFileName(testApp); archiveFile.generateArchive(); TarArchiveInputStream tarInputStream = null; List<String> names = new ArrayList<String>(); File decryptedFile = null; try { decryptedFile = decrypt(new File(fileName)); tarInputStream = new TarArchiveInputStream(new FileInputStream(decryptedFile)); TarArchiveEntry entry = null; while ((entry = tarInputStream.getNextTarEntry()) != null) { names.add(entry.getName()); } } finally { IOUtils.closeQuietly(tarInputStream); } Assert.assertEquals(2, names.size()); Assert.assertTrue("Student extract file not found", names.get(1).contains("student")); Assert.assertTrue("Metadata file not found", names.get(0).contains("metadata")); FileUtils.deleteQuietly(decryptedFile); }
From source file:org.soulwing.credo.service.archive.TarGzipArchiveBuilderTest.java
@Test public void testBuildArchiveWithSingleEntry() throws Exception { byte[] archive = builder.beginEntry("file.txt", "UTF-8").addContent(new StringReader("content")).endEntry() .build();//from www . java2 s.c o m TarArchiveInputStream tis = new TarArchiveInputStream( new GzipCompressorInputStream(new ByteArrayInputStream(archive))); TarArchiveEntry entry = tis.getNextTarEntry(); assertThat(entry.getMode(), is(equalTo(0100400))); assertThat(entry.getName(), is(equalTo("file.txt"))); assertThat(readContent(tis), is(equalTo("content"))); assertThat(tis.getNextEntry(), is(nullValue())); }