org.apache.commons.compress.archivers.ArchiveException Java Examples

The following examples show how to use org.apache.commons.compress.archivers.ArchiveException. 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: DownloadServlet.java    From document-management-system with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Generate a jar file from a repository folder path
 */
private void exportFolderAsJar(String fldPath, OutputStream os) throws PathNotFoundException, AccessDeniedException,
		RepositoryException, ArchiveException, ParseException, NoSuchGroupException, IOException, DatabaseException, MessagingException {
	log.debug("exportFolderAsJar({}, {})", fldPath, os);
	StringWriter out = new StringWriter();
	File tmp = null;

	try {
		tmp = FileUtils.createTempDir();

		// Export files
		RepositoryExporter.exportDocuments(null, fldPath, tmp, false, false, out, new TextInfoDecorator(fldPath));

		// Jar files
		ArchiveUtils.createJar(tmp, PathUtils.getName(fldPath), os);
	} catch (IOException e) {
		log.error("Error exporting jar", e);
		throw e;
	} finally {
		IOUtils.closeQuietly(out);
		FileUtils.deleteQuietly(tmp);
	}

	log.debug("exportFolderAsJar: void");
}
 
Example #2
Source File: LogUtils.java    From konduit-serving with Apache License 2.0 6 votes vote down vote up
public static File getZippedLogs() throws ArchiveException, IOException {
    File zippedFile = new File(DirectoryFetcher.getEndpointLogsDir(), "logs.zip");

    try (BufferedOutputStream archiveStream = new BufferedOutputStream(new FileOutputStream(zippedFile))) {
        try (ArchiveOutputStream archive = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, archiveStream)) {
            File logsFile = getEndpointLogsFile();

            if(logsFile != null) {
                ZipArchiveEntry entry = new ZipArchiveEntry(logsFile.getName());
                archive.putArchiveEntry(entry);

                try (BufferedInputStream input = new BufferedInputStream(new FileInputStream(logsFile))) {
                    IOUtils.copy(input, archive);
                    archive.closeArchiveEntry();
                    archive.finish();
                }
            } else {
                throw new FileNotFoundException("No logs file found!");
            }
        }
    }

    return zippedFile;
}
 
Example #3
Source File: ComposerJsonExtractor.java    From nexus-repository-composer with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Extracts the contents for the first matching {@code composer.json} file (of which there should only be one) as a
 * map representing the parsed JSON content. If no such file is found then an empty map is returned.
 */
public Map<String, Object> extractFromZip(final Blob blob) throws IOException {
  try (InputStream is = blob.getInputStream()) {
    try (ArchiveInputStream ais = archiveStreamFactory.createArchiveInputStream(ArchiveStreamFactory.ZIP, is)) {
      ArchiveEntry entry = ais.getNextEntry();
      while (entry != null) {
        Map<String, Object> contents = processEntry(ais, entry);
        if (!contents.isEmpty()) {
          return contents;
        }
        entry = ais.getNextEntry();
      }
    }
    return Collections.emptyMap();
  }
  catch (ArchiveException e) {
    throw new IOException("Error reading from archive", e);
  }
}
 
Example #4
Source File: ZipTest.java    From document-management-system with GNU General Public License v2.0 6 votes vote down vote up
public void testApache() throws IOException, ArchiveException {
	log.debug("testApache()");
	File zip = File.createTempFile("apache_", ".zip");
	
	// Create zip
	FileOutputStream fos = new FileOutputStream(zip);
	ArchiveOutputStream aos = new ArchiveStreamFactory().createArchiveOutputStream("zip", fos);
	aos.putArchiveEntry(new ZipArchiveEntry("coñeta"));
	aos.closeArchiveEntry();
	aos.close();
	
	// Read zip
	FileInputStream fis = new FileInputStream(zip);
	ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream("zip", fis);
	ZipArchiveEntry zae = (ZipArchiveEntry) ais.getNextEntry();
	assertEquals(zae.getName(), "coñeta");
	ais.close();
}
 
Example #5
Source File: RDescriptionUtils.java    From nexus-repository-r with Eclipse Public License 1.0 6 votes vote down vote up
private static Map<String, String> extractMetadataFromArchive(final String archiveType, final InputStream is) {
  final ArchiveStreamFactory archiveFactory = new ArchiveStreamFactory();
  try (ArchiveInputStream ais = archiveFactory.createArchiveInputStream(archiveType, is)) {
    ArchiveEntry entry = ais.getNextEntry();
    while (entry != null) {
      if (!entry.isDirectory() && DESCRIPTION_FILE_PATTERN.matcher(entry.getName()).matches()) {
        return parseDescriptionFile(ais);
      }
      entry = ais.getNextEntry();
    }
  }
  catch (ArchiveException | IOException e) {
    throw new RException(null, e);
  }
  throw new IllegalStateException("No metadata file found");
}
 
Example #6
Source File: TarGzCompressionUtilsTest.java    From incubator-pinot with Apache License 2.0 6 votes vote down vote up
@Test
public void testBasic()
    throws IOException, InterruptedException, ArchiveException {
  File metaFile = new File(segmentDir, "metadata.properties");
  metaFile.createNewFile();
  File tarGzPath = new File(tarDir, SEGMENT_NAME + ".tar.gz");
  TarGzCompressionUtils.createTarGzOfDirectory(segmentDir.getPath(), tarGzPath.getPath(), "segmentId_");
  TarGzCompressionUtils.unTar(tarGzPath, untarDir);
  File[] files = untarDir.listFiles();
  Assert.assertNotNull(files);
  Assert.assertEquals(files.length, 1);
  File[] subFiles = files[0].listFiles();
  Assert.assertNotNull(subFiles);
  Assert.assertEquals(subFiles.length, 1);
  Assert.assertEquals(subFiles[0].getName(), "metadata.properties");
}
 
Example #7
Source File: AzkabanJobHelper.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
@edu.umd.cs.findbugs.annotations.SuppressWarnings(
    value = "OBL_UNSATISFIED_OBLIGATION",
    justification = "Lombok construct of @Cleanup is handing this, but not detected by FindBugs")
private static void addFilesToZip(File zipFile, List<File> filesToAdd) throws IOException {
  try {
    @Cleanup
    OutputStream archiveStream = new FileOutputStream(zipFile);
    @Cleanup
    ArchiveOutputStream archive =
        new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, archiveStream);

    for (File fileToAdd : filesToAdd) {
      ZipArchiveEntry entry = new ZipArchiveEntry(fileToAdd.getName());
      archive.putArchiveEntry(entry);

      @Cleanup
      BufferedInputStream input = new BufferedInputStream(new FileInputStream(fileToAdd));
      IOUtils.copy(input, archive);
      archive.closeArchiveEntry();
    }

    archive.finish();
  } catch (ArchiveException e) {
    throw new IOException("Issue with creating archive", e);
  }
}
 
Example #8
Source File: CompressExample.java    From spring-boot with Apache License 2.0 6 votes vote down vote up
public static void makeOnlyUnZip() throws ArchiveException, IOException{
		final InputStream is = new FileInputStream("D:/中文名字.zip");
		ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream(ArchiveStreamFactory.ZIP, is);
		ZipArchiveEntry entry = entry = (ZipArchiveEntry) in.getNextEntry();
		
		String dir = "D:/cnname";
		File filedir = new File(dir);
		if(!filedir.exists()){
			filedir.mkdir();
		}
//		OutputStream out = new FileOutputStream(new File(dir, entry.getName()));
		OutputStream out = new FileOutputStream(new File(filedir, entry.getName()));
		IOUtils.copy(in, out);
		out.close();
		in.close();
	}
 
Example #9
Source File: CompressExample.java    From spring-boot with Apache License 2.0 6 votes vote down vote up
public static void makeOnlyZip() throws IOException, ArchiveException{
	File f1 = new File("D:/compresstest.txt");
       File f2 = new File("D:/test1.xml");
       
       final OutputStream out = new FileOutputStream("D:/中文名字.zip");
       ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, out);
       
       os.putArchiveEntry(new ZipArchiveEntry(f1.getName()));
       IOUtils.copy(new FileInputStream(f1), os);
       os.closeArchiveEntry();

       os.putArchiveEntry(new ZipArchiveEntry(f2.getName()));
       IOUtils.copy(new FileInputStream(f2), os);
       os.closeArchiveEntry();
       os.close();
}
 
Example #10
Source File: ProcessingManagerTest.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
@Test
public void system_size() throws IOException, CompressorException, ArchiveException
{
   ProcessingManager mgr = new ProcessingManager();
   long size = mgr.system_size(sample);
   Assert.assertEquals(size, 494928);
   
   File folder=sample.getParentFile();
   File extaction_folder=new File(folder, "unzip");
   extaction_folder.mkdirs();
   
   UnZip.unCompress(sample.getAbsolutePath(), 
      extaction_folder.getAbsolutePath());
   
   File tocheck = extaction_folder.listFiles()[0];
   size = mgr.system_size(tocheck);
   Assert.assertEquals(size, SIZE, tocheck.getAbsolutePath());
}
 
Example #11
Source File: ZipFileUtils.java    From kylin with Apache License 2.0 5 votes vote down vote up
public static void decompressZipfileToDirectory(String zipFileName, File outputFolder)
        throws IOException, ArchiveException {
    if (!validateZipFilename(zipFileName)) {
        throw new RuntimeException("Zip file must end with .zip");
    }
    Expander expander = new Expander();
    ZipFile zipFile = new ZipFile(zipFileName);
    expander.expand(zipFile, outputFolder);
}
 
Example #12
Source File: ArchiveFileTree.java    From gradle-plugins with MIT License 5 votes vote down vote up
@Override
@SneakyThrows(IOException.class)
public InputStream open() {
    if (!closed && !inputStreamUsed) {
        // We are still visiting this FTE, so we can use the overall ArchiveInputStream
        return new ArchiveEntryInputStream(this);
    }
    // We already used the the overall ArchiveInputStream or it has moved to another entry
    // so we have to open a new InputStream

    // If getFile() was called before, we can use the file to open a new InputStream.
    if (file != null && file.exists()) {
        return new FileInputStream(file);
    }

    // As last resort: Reopen the Archive and skip forward to our ArchiveEntry
    try {
        IS archiveInputStream = inputStreamProvider.openFile(archiveFile);

        ArchiveEntry tmp;

        while ((tmp = archiveInputStream.getNextEntry()) != null) {
            if (tmp.equals(archiveEntry)) {
                return archiveInputStream;
            }
        }

        throw new IOException(archiveEntry.getName() + " not found in " + archiveFile);

    } catch (ArchiveException e) {
        throw new IOException(e);
    }
}
 
Example #13
Source File: TraceImportOperationTest.java    From tracecompass with Eclipse Public License 2.0 5 votes vote down vote up
private static void createArchive(String sourcePath, File archive) throws FileNotFoundException, IOException, ArchiveException {
    try (OutputStream out = new FileOutputStream(archive);
            ArchiveOutputStream archiveOutputStream = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, out)) {
        for (File file : FileUtils.listFiles(new File(sourcePath), null, true)) {
            String name = file.getAbsolutePath().substring(sourcePath.length());
            archiveOutputStream.putArchiveEntry(new ZipArchiveEntry(name));
            try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(file))) {
                IOUtils.copy(in, archiveOutputStream);
            }
            archiveOutputStream.closeArchiveEntry();
        }
        archiveOutputStream.finish();
    }
}
 
Example #14
Source File: CompressionDataParser.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public InputStream getNextInputStream() throws IOException {
  if(archiveInputStream == null) {
    // Very first call to getNextInputStream, initialize archiveInputStream using the wrappedOffset
    wrappedOffset = wrappedOffset.equals(ZERO) ? wrapOffset(wrappedOffset) : wrappedOffset;
    Map<String, Object> archiveInputOffset = objectMapper.readValue(wrappedOffset, Map.class);
    try {
      archiveInputStream = new ArchiveStreamFactory().createArchiveInputStream(
          new BufferedInputStream(compressionInput.getNextInputStream()));
    } catch (ArchiveException e) {
      throw new IOException(e);
    }
    seekToOffset(archiveInputOffset);
    nextInputStream = archiveInputStream;
  }

  if (nextInputStream == null) {
    // this means reached end of a compressed file within the archive. seek to the next eligible entry
    seekToNextEligibleEntry();
    if (currentEntry != null) {
      // Not end of archive
      nextInputStream = archiveInputStream;
    }
  }
  InputStream temp = nextInputStream;
  nextInputStream = null;
  return temp;
}
 
Example #15
Source File: ZipFileUtils.java    From kylin with Apache License 2.0 5 votes vote down vote up
public static void compressZipFile(String sourceDir, String zipFileName) throws IOException, ArchiveException {
    if (!validateZipFilename(zipFileName)) {
        throw new RuntimeException("Zip file must end with .zip");
    }
    Archiver archiver = new Archiver();
    archiver.create(ArchiveStreamFactory.ZIP, new File(zipFileName), new File(sourceDir));
}
 
Example #16
Source File: ZipFileUtils.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public static void decompressZipfileToDirectory(String zipFileName, File outputFolder)
        throws IOException, ArchiveException {
    if (!validateZipFilename(zipFileName)) {
        throw new RuntimeException("Zip file must end with .zip");
    }
    Expander expander = new Expander();
    ZipFile zipFile = new ZipFile(zipFileName);
    expander.expand(zipFile, outputFolder);
}
 
Example #17
Source File: TarGzCompressionUtilsTest.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
@Test
public void testSubDirectories()
    throws IOException, ArchiveException, InterruptedException {
  new File(segmentDir, "metadata.properties").createNewFile();
  File v3Dir = new File(segmentDir, "v3");
  FileUtils.forceMkdir(v3Dir);
  new File(v3Dir, "creation.meta").createNewFile();

  File tarGzPath = new File(tarDir, SEGMENT_NAME + ".tar.gz");
  TarGzCompressionUtils.createTarGzOfDirectory(segmentDir.getPath(), tarGzPath.getPath());
  TarGzCompressionUtils.unTar(tarGzPath, untarDir);
  File[] segments = untarDir.listFiles();
  Assert.assertNotNull(segments);
  Assert.assertEquals(segments.length, 1);
  File[] segmentFiles = segments[0].listFiles();
  Assert.assertNotNull(segmentFiles);
  Assert.assertEquals(segmentFiles.length, 2);
  for (File segmentFile : segmentFiles) {
    String name = segmentFile.getName();
    Assert.assertTrue(name.equals("v3") || name.equals("metadata.properties"));
    if (name.equals("v3")) {
      Assert.assertTrue(segmentFile.isDirectory());
      File[] v3Files = v3Dir.listFiles();
      Assert.assertNotNull(v3Files);
      Assert.assertEquals(v3Files.length, 1);
      Assert.assertEquals(v3Files[0].getName(), "creation.meta");
    }
  }
}
 
Example #18
Source File: TarGzCompressionUtilsTest.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
@Test
public void testEmptyDirectory()
    throws IOException, ArchiveException {
  File tarGzPath = new File(tarDir, SEGMENT_NAME + ".tar.gz");
  TarGzCompressionUtils.createTarGzOfDirectory(segmentDir.getPath(), tarGzPath.getPath());
  TarGzCompressionUtils.unTar(tarGzPath, untarDir);
  File[] segments = untarDir.listFiles();
  Assert.assertNotNull(segments);
  Assert.assertEquals(segments.length, 1);
  File[] segmentFiles = segments[0].listFiles();
  Assert.assertNotNull(segmentFiles);
  Assert.assertEquals(segmentFiles.length, 0);
}
 
Example #19
Source File: Archives.java    From wildfly-maven-plugin with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Unzips the zip file to the target directory.
 * <p>
 * Note this is specific to how WildFly is archived. The first directory is assumed to be the base home directory
 * and will returned.
 * </p>
 *
 * @param archiveFile     the archive to uncompress, can be a {@code .zip} or {@code .tar.gz}
 * @param targetDir       the directory to extract the zip file to
 * @param replaceIfExists if {@code true} replace the existing files if they exist
 *
 * @return the path to the extracted directory
 *
 * @throws java.io.IOException if an I/O error occurs
 */
@SuppressWarnings("WeakerAccess")
public static Path uncompress(final Path archiveFile, final Path targetDir, final boolean replaceIfExists) throws IOException {
    final Path archive = getArchive(archiveFile);

    Path firstDir = null;

    try (ArchiveInputStream in = new ArchiveStreamFactory().createArchiveInputStream(new BufferedInputStream(Files.newInputStream(archive)))) {
        ArchiveEntry entry;
        while ((entry = in.getNextEntry()) != null) {
            final Path extractTarget = targetDir.resolve(entry.getName());
            if (!replaceIfExists && Files.exists(extractTarget)) {
                if (entry.isDirectory() && firstDir == null) {
                    firstDir = extractTarget;
                }
                continue;
            }
            if (entry.isDirectory()) {
                final Path dir = Files.createDirectories(extractTarget);
                if (firstDir == null) {
                    firstDir = dir;
                }
            } else {
                Files.createDirectories(extractTarget.getParent());
                Files.copy(in, extractTarget);
            }
        }
        return firstDir == null ? targetDir : firstDir;
    } catch (ArchiveException e) {
        throw new IOException(e);
    }
}
 
Example #20
Source File: CompressUtil.java    From dependency-track with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method that attempts to automatically identify an archive and its type,
 * extract the contents as a byte array. If this fails, it will gracefully return
 * the original input byte array without exception. If the input was not an archive
 * or compressed, it will return the original byte array.
 * @param input the
 * @return a byte array
 */
public static byte[] optionallyDecompress(final byte[] input) {
    try (final ByteArrayInputStream bis = new ByteArrayInputStream(input);
         final ArchiveInputStream ais = new ArchiveStreamFactory().createArchiveInputStream(bis)) {
        final ArchiveEntry entry = ais.getNextEntry();
        if (ais.canReadEntryData(entry)) {
            return IOUtils.toByteArray(ais);
        }
    } catch (ArchiveException | IOException e) {
        // throw it away and return the original byte array
    }
    return input;
}
 
Example #21
Source File: ZipFileUtils.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public static void compressZipFile(String sourceDir, String zipFileName) throws IOException, ArchiveException {
    if (!validateZipFilename(zipFileName)) {
        throw new RuntimeException("Zip file must end with .zip");
    }
    Archiver archiver = new Archiver();
    archiver.create(ArchiveStreamFactory.ZIP, new File(zipFileName), new File(sourceDir));
}
 
Example #22
Source File: DownloadServlet.java    From document-management-system with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generate a zip file from a repository folder path
 */
private void exportFolderAsZip(String fldPath, OutputStream os) throws PathNotFoundException, AccessDeniedException,
		RepositoryException, ArchiveException, ParseException, NoSuchGroupException, IOException, DatabaseException, MessagingException {
	log.debug("exportFolderAsZip({}, {})", fldPath, os);
	StringWriter out = new StringWriter();
	FileOutputStream fos = null;
	InputStream is = null;
	File tmp = null;

	try {
		tmp = FileUtils.createTempDir();

		if (fldPath.startsWith("/" + Repository.CATEGORIES)) {
			String categoryId = OKMRepository.getInstance().getNodeUuid(null, fldPath);

			for (Document doc : OKMSearch.getInstance().getCategorizedDocuments(null, categoryId)) {
				is = OKMDocument.getInstance().getContent(null, doc.getUuid(), false);
				fos = new FileOutputStream(new File(tmp, PathUtils.getName(doc.getPath())));
				IOUtils.copy(is, fos);
				IOUtils.closeQuietly(is);
				IOUtils.closeQuietly(fos);
			}
		} else {
			RepositoryExporter.exportDocuments(null, fldPath, tmp, false, false, out, new TextInfoDecorator(fldPath));
		}

		// Zip files
		ArchiveUtils.createZip(tmp, PathUtils.getName(fldPath), os);
	} catch (IOException e) {
		log.error("Error exporting zip", e);
		throw e;
	} finally {
		IOUtils.closeQuietly(is);
		IOUtils.closeQuietly(fos);
		IOUtils.closeQuietly(out);
		FileUtils.deleteQuietly(tmp);
	}

	log.debug("exportFolderAsZip: void");
}
 
Example #23
Source File: CompressExample.java    From spring-boot with Apache License 2.0 5 votes vote down vote up
/**
	 * @param args
	 */
	public static void main(String[] args) throws ArchiveException {
		try {
//			makeZip();
//			makeOnlyZip();
//			makeOnlyUnZip();
//			makeUnZip();
			unZipToFolder("D:\\中文名字.zip", "D:\\中文哈");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
 
Example #24
Source File: CompressExample.java    From spring-boot with Apache License 2.0 5 votes vote down vote up
public static void makeZip() throws IOException, ArchiveException{
		File f1 = new File("D:/compresstest.txt");
        File f2 = new File("D:/test1.xml");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        
        //ArchiveOutputStream ostemp = new ArchiveStreamFactory().createArchiveOutputStream("zip", baos);
        ZipArchiveOutputStream ostemp = new ZipArchiveOutputStream(baos);
        ostemp.setEncoding("GBK");
        ostemp.putArchiveEntry(new ZipArchiveEntry(f1.getName()));
        IOUtils.copy(new FileInputStream(f1), ostemp);
        ostemp.closeArchiveEntry();
        ostemp.putArchiveEntry(new ZipArchiveEntry(f2.getName()));
        IOUtils.copy(new FileInputStream(f2), ostemp);
        ostemp.closeArchiveEntry();
        ostemp.finish();
        ostemp.close();

//      final OutputStream out = new FileOutputStream("D:/testcompress.zip");
        final OutputStream out = new FileOutputStream("D:/中文名字.zip");
        ArchiveOutputStream os = new ArchiveStreamFactory().createArchiveOutputStream("zip", out);
        os.putArchiveEntry(new ZipArchiveEntry("打包.zip"));
        baos.writeTo(os);
        os.closeArchiveEntry();
        baos.close();
        os.finish();
        os.close();
	}
 
Example #25
Source File: ArchiveFileTree.java    From gradle-plugins with MIT License 5 votes vote down vote up
@Override
@SneakyThrows(IOException.class)
public InputStream open() {
    if (!closed && !inputStreamUsed) {
        // We are still visiting this FTE, so we can use the overall ArchiveInputStream
        return new ArchiveEntryInputStream(this);
    }
    // We already used the the overall ArchiveInputStream or it has moved to another entry
    // so we have to open a new InputStream

    // If getFile() was called before, we can use the file to open a new InputStream.
    if (file != null && file.exists()) {
        return new FileInputStream(file);
    }

    // As last resort: Reopen the Archive and skip forward to our ArchiveEntry
    try {
        IS archiveInputStream = inputStreamProvider.openFile(archiveFile);

        ArchiveEntry tmp;

        while ((tmp = archiveInputStream.getNextEntry()) != null) {
            if (tmp.equals(archiveEntry)) {
                return archiveInputStream;
            }
        }

        throw new IOException(archiveEntry.getName() + " not found in " + archiveFile);

    } catch (ArchiveException e) {
        throw new IOException(e);
    }
}
 
Example #26
Source File: RNNodeService.java    From react-native-node with MIT License 5 votes vote down vote up
private static List<File> unTar(final File inputFile, final File outputDir) throws FileNotFoundException, IOException, ArchiveException {
    Log.i(TAG, String.format("Untaring %s to dir %s", inputFile.getAbsolutePath(), outputDir.getAbsolutePath()));

    if (!outputDir.exists()) {
        outputDir.mkdirs();
    }
    final List<File> untaredFiles = new LinkedList<File>();
    final InputStream is = new FileInputStream(inputFile);
    final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is);
    TarArchiveEntry entry = null;
    while ((entry = (TarArchiveEntry)debInputStream.getNextEntry()) != null) {
        final File outputFile = new File(outputDir, entry.getName());
        if (entry.isDirectory()) {
            if (!outputFile.exists()) {
                if (!outputFile.mkdirs()) {
                    throw new IllegalStateException(String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
                }
            }
        } else {
            final OutputStream outputFileStream = new FileOutputStream(outputFile);
            IOUtils.copy(debInputStream, outputFileStream);
            outputFileStream.close();
        }
        untaredFiles.add(outputFile);
    }
    debInputStream.close();

    return untaredFiles;
}
 
Example #27
Source File: CompressionUtils.java    From waterdrop with Apache License 2.0 5 votes vote down vote up
/** Untar an input file into an output file.

     * The output file is created in the output folder, having the same name
     * as the input file, minus the '.tar' extension.
     *
     * @param inputFile     the input .tar file
     * @param outputDir     the output directory file.
     * @throws IOException
     * @throws FileNotFoundException
     *
     * @return  The {@link List} of {@link File}s with the untared content.
     * @throws ArchiveException
     */
    public static List<File> unTar(final File inputFile, final File outputDir) throws FileNotFoundException, IOException, ArchiveException {

        logger.info(String.format("Untaring %s to dir %s.", inputFile.getAbsolutePath(), outputDir.getAbsolutePath()));

        final List<File> untaredFiles = new LinkedList<File>();
        final InputStream is = new FileInputStream(inputFile);
        final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory().createArchiveInputStream("tar", is);
        TarArchiveEntry entry = null;
        while ((entry = (TarArchiveEntry)debInputStream.getNextEntry()) != null) {
            final File outputFile = new File(outputDir, entry.getName());
            if (entry.isDirectory()) {
                logger.info(String.format("Attempting to write output directory %s.", outputFile.getAbsolutePath()));
                if (!outputFile.exists()) {
                    logger.info(String.format("Attempting to create output directory %s.", outputFile.getAbsolutePath()));
                    if (!outputFile.mkdirs()) {
                        throw new IllegalStateException(String.format("Couldn't create directory %s.", outputFile.getAbsolutePath()));
                    }
                }
            } else {
                logger.info(String.format("Creating output file %s.", outputFile.getAbsolutePath()));
                final OutputStream outputFileStream = new FileOutputStream(outputFile);
                IOUtils.copy(debInputStream, outputFileStream);
                outputFileStream.close();
            }
            untaredFiles.add(outputFile);
        }
        debInputStream.close();

        return untaredFiles;
    }
 
Example #28
Source File: FileUtil.java    From teku with Apache License 2.0 5 votes vote down vote up
public static void unTar(final File inputTarFile, final File outputDir)
    throws IOException, ArchiveException {
  final InputStream fileStream = new FileInputStream(inputTarFile);
  try (final ArchiveInputStream archiveStream =
      new ArchiveStreamFactory().createArchiveInputStream("tar", fileStream)) {
    ArchiveEntry entry;
    while ((entry = archiveStream.getNextEntry()) != null) {
      final File outputFile = new File(outputDir, entry.getName());
      if (entry.isDirectory() && !outputFile.exists()) {
        // Create directory
        if (!outputFile.mkdirs()) {
          throw new IllegalStateException(
              "Could not create directory " + outputFile.getAbsolutePath());
        }
      } else if (!entry.isDirectory()) {
        // Make sure parent directories exist
        File parent = outputFile.getParentFile();
        if (!parent.isDirectory() && !parent.mkdirs()) {
          throw new IllegalStateException(
              "Could not create directory " + parent.getAbsolutePath());
        }
        // Create file
        try (final OutputStream outputFileStream = new FileOutputStream(outputFile)) {
          IOUtils.copy(archiveStream, outputFileStream);
        }
      }
    }
  }
}
 
Example #29
Source File: DownloadServlet.java    From document-management-system with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generate a zip file from a list of documents
 */
private void exportDocumentsAsZip(List<String> paths, OutputStream os, String zipname) throws PathNotFoundException,
		AccessDeniedException, RepositoryException, ArchiveException, ParseException, NoSuchGroupException, IOException,
		DatabaseException {
	log.debug("exportDocumentsAsZip({}, {})", paths, os);
	StringWriter out = new StringWriter();
	File tmp = null;

	try {
		tmp = FileUtils.createTempDir();
		File fsPath = new File(tmp.getPath());

		// Export files
		for (String docPath : paths) {
			String destPath = fsPath.getPath() + File.separator + PathUtils.getName(docPath).replace(':', '_');
			RepositoryExporter.exportDocument(null, docPath, destPath, false, false, out, new TextInfoDecorator(docPath));
		}

		// Zip files
		ArchiveUtils.createZip(tmp, zipname, os);
	} catch (IOException e) {
		log.error("Error exporting zip", e);
		throw e;
	} finally {
		IOUtils.closeQuietly(out);
		FileUtils.deleteQuietly(tmp);
	}

	log.debug("exportDocumentsAsZip: void");
}
 
Example #30
Source File: BackfillControllerAPIs.java    From incubator-pinot with Apache License 2.0 4 votes vote down vote up
/**
 * Downloads a segment from the controller, given the table name and segment name
 * @param segmentName
 * @param hdfsSegmentPath
 * @throws IOException
 * @throws ArchiveException
 */
public void downloadSegment(String segmentName, Path hdfsSegmentPath)
    throws IOException, ArchiveException {

  FileSystem fs = FileSystem.get(new Configuration());
  HttpClient controllerClient = new DefaultHttpClient();
  HttpGet req = new HttpGet(SEGMENTS_ENDPOINT + URLEncoder.encode(tableName, UTF_8)
      + "/" + URLEncoder.encode(segmentName, UTF_8));
  HttpResponse res = controllerClient.execute(controllerHttpHost, req);
  try {
    if (res.getStatusLine().getStatusCode() != 200) {
      throw new IllegalStateException(res.getStatusLine().toString());
    }
    LOGGER.info("Fetching segment {}", segmentName);
    InputStream content = res.getEntity().getContent();

    File tempDir = new File(Files.createTempDir(), "thirdeye_temp");
    tempDir.mkdir();
    LOGGER.info("Creating temporary dir for staging segments {}", tempDir);
    File tempSegmentDir = new File(tempDir, segmentName);
    File tempSegmentTar = new File(tempDir, segmentName + ThirdEyeConstants.TAR_SUFFIX);

    LOGGER.info("Downloading {} to {}", segmentName, tempSegmentTar);
    OutputStream out = new FileOutputStream(tempSegmentTar);
    IOUtils.copy(content, out);
    if (!tempSegmentTar.exists()) {
      throw new IllegalStateException("Download of " + segmentName + " unsuccessful");
    }

    LOGGER.info("Extracting segment {} to {}", tempSegmentTar, tempDir);
    TarGzCompressionUtils.unTar(tempSegmentTar, tempDir);
    File[] files = tempDir.listFiles(new FilenameFilter() {

      @Override
      public boolean accept(File dir, String name) {
        return !name.endsWith(ThirdEyeConstants.TAR_SUFFIX) && new File(dir, name).isDirectory();
      }
    });
    if (files.length == 0) {
      throw new IllegalStateException("Failed to extract " + tempSegmentTar + " to " + tempDir);
    } else if (!files[0].getName().equals(tempSegmentDir.getName())){
      LOGGER.info("Moving extracted segment to the segment dir {}", tempSegmentDir);
      FileUtils.moveDirectory(files[0], tempSegmentDir);
    }
    if (!tempSegmentDir.exists()) {
      throw new IllegalStateException("Failed to move " + files[0] + " to " + tempSegmentDir);
    }

    LOGGER.info("Copying segment from {} to hdfs {}", tempSegmentDir, hdfsSegmentPath);
    fs.copyFromLocalFile(new Path(tempSegmentDir.toString()), hdfsSegmentPath);
    Path hdfsSegmentDir = new Path(hdfsSegmentPath, segmentName);
    if (!fs.exists(hdfsSegmentDir)) {
      throw new IllegalStateException("Failed to copy segment " + segmentName + " from local path " + tempSegmentDir
          + " to hdfs path " + hdfsSegmentPath);
    }
  } finally {
    if (res.getEntity() != null) {
      EntityUtils.consume(res.getEntity());
    }
  }
  LOGGER.info("Successfully downloaded segment {} to {}", segmentName, hdfsSegmentPath);
}