本文整理匯總了Java中org.apache.commons.compress.archivers.ArchiveInputStream類的典型用法代碼示例。如果您正苦於以下問題:Java ArchiveInputStream類的具體用法?Java ArchiveInputStream怎麽用?Java ArchiveInputStream使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
ArchiveInputStream類屬於org.apache.commons.compress.archivers包,在下文中一共展示了ArchiveInputStream類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: makeOnlyUnZip
import org.apache.commons.compress.archivers.ArchiveInputStream; //導入依賴的package包/類
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();
}
示例2: testApache
import org.apache.commons.compress.archivers.ArchiveInputStream; //導入依賴的package包/類
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();
}
示例3: copyArchiveContents
import org.apache.commons.compress.archivers.ArchiveInputStream; //導入依賴的package包/類
/**
* Copies the contents of an archive file to the specified output stream
*
* @param archiveFile the archive file
* @param outputStream the output stream
* @throws IOException error copying the archive contents
*/
private void copyArchiveContents(File archiveFile, ArchiveOutputStream outputStream) throws IOException {
ArchiveInputStream inputStream = this.createArchiveInputStream(archiveFile);
ArchiveEntry entry;
byte[] buffer = new byte[this.bufferSize];
while ((entry = inputStream.getNextEntry()) != null) {
outputStream.putArchiveEntry(entry);
int count;
while ((count = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, count);
}
outputStream.closeArchiveEntry();
}
}
示例4: extractMetadataFromArchive
import org.apache.commons.compress.archivers.ArchiveInputStream; //導入依賴的package包/類
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");
}
示例5: readTap
import org.apache.commons.compress.archivers.ArchiveInputStream; //導入依賴的package包/類
private void readTap(TestHarnessReportBuilder builder, ArchiveInputStream archive, ArchiveEntry entry) {
BufferedReader br = new BufferedReader(new InputStreamReader(archive));
TestDetailBuilder detailBuilder = TestDetail.builder();
detailBuilder.filePath(entry.getName());
br.lines().forEach(line -> {
if (line.startsWith("ok ")) {
detailBuilder.ok();
} else if (line.startsWith("not ok ")) {
detailBuilder.failed();
} else if (line.startsWith("1..")) {
Matcher m = tapNumberOfTests.matcher(line);
if (m.matches()) {
detailBuilder.total(Integer.valueOf(m.group(1)));
}
}
});
TestDetail detail = detailBuilder.build();
if (detail.getNumberOfTests() > 0) {
builder.addTestDetail(detail);
} else {
log.info("Did not recognize TAP or tests skipped completely: " + detail.getFilePath());
}
}
示例6: readMetaYaml
import org.apache.commons.compress.archivers.ArchiveInputStream; //導入依賴的package包/類
@SuppressWarnings("unchecked")
private void readMetaYaml(TestHarnessReport.TestHarnessReportBuilder builder, ArchiveInputStream archive) throws YamlException {
BufferedReader br = new BufferedReader(new InputStreamReader(archive));
YamlReader reader = new YamlReader(br);
Map<String, Object> object = (Map<String, Object>) reader.read(Map.class);
builder.startTime(getFromMap(object, "start_time"));
builder.endTime(getFromMap(object, "stop_time"));
for (Map<String, Object> fileAttr : (List<Map<String, Object>>) object.get("file_attributes")) {
builder.addTest(
new Test((String) fileAttr.get("description"), new BigDecimal((String) fileAttr.get("start_time")),
new BigDecimal((String) fileAttr.get("end_time"))));
}
}
示例7: createArchiveInputStream
import org.apache.commons.compress.archivers.ArchiveInputStream; //導入依賴的package包/類
/**
* Create a new ArchiveInputStream to read an archive file based on a format
* parameter.
*
* If format is not set, this method tries to detect file format
* automatically. In this case, BufferedInputStream is used to wrap
* FileInputInputStream instance. BufferedInputStream may read a data
* partially when calling files.nextFile(). However, it doesn't matter
* because the partial read data should be discarded. And then this method
* is called again to create a new ArchiveInputStream.
*
* @return a new ArchiveInputStream instance.
*/
ArchiveInputStream createArchiveInputStream(String format, InputStream in)
throws IOException, ArchiveException {
ArchiveStreamFactory factory = new ArchiveStreamFactory();
if (CommonsCompressUtil.isAutoDetect(format)) {
in = in.markSupported() ? in : new BufferedInputStream(in);
try {
return factory.createArchiveInputStream(in);
} catch (ArchiveException e) {
throw new IOException(
"Failed to detect a file format. Please try to set a format explicitly.",
e);
}
} else {
return factory.createArchiveInputStream(format, in);
}
}
示例8: testArchiveFile
import org.apache.commons.compress.archivers.ArchiveInputStream; //導入依賴的package包/類
@Test
public void testArchiveFile() throws Exception {
InputStream in = getClass().getResourceAsStream("samples.tar");
ArchiveInputStream ain = new ArchiveStreamFactory().createArchiveInputStream(in);
ArchiveInputStreamIterator it = new ArchiveInputStreamIterator(ain);
assertTrue("Verify there is 1st item.", it.hasNext());
assertEquals("Verify ArchiveInputStream is return.", "1,foo", readContents(it.next()));
assertTrue("Verify there is 2nd item.", it.hasNext());
assertEquals("Verify ArchiveInputStream is return.", "2,bar", readContents(it.next()));
assertFalse("Verify there is no next item.", it.hasNext());
assertNull("Verify there is no stream.", it.next());
// Verify calling after no data.
assertFalse("Verify there is no next item.", it.hasNext());
assertNull("Verify there is no stream.", it.next());
}
示例9: extractArchive
import org.apache.commons.compress.archivers.ArchiveInputStream; //導入依賴的package包/類
private static void extractArchive(final File destination, final ArchiveInputStream archiveInputStream)
throws IOException {
final int BUFFER_SIZE = 8192;
ArchiveEntry entry = archiveInputStream.getNextEntry();
while (entry != null) {
final File destinationFile = new File(destination, entry.getName());
if (entry.isDirectory()) {
destinationFile.mkdir();
} else {
destinationFile.getParentFile().mkdirs();
try (final OutputStream fileOutputStream = new FileOutputStream(destinationFile)) {
final byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead;
while ((bytesRead = archiveInputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, bytesRead);
}
}
}
entry = archiveInputStream.getNextEntry();
}
}
示例10: extractTarOrGZToTempDir
import org.apache.commons.compress.archivers.ArchiveInputStream; //導入依賴的package包/類
private boolean extractTarOrGZToTempDir(File nestedTempDir, File tar, String virtualPath) {
ArchiveInputStream tarInputStream = null;
try {
tarInputStream = getArchiveInputStream(tar);
ArchiveEntry entry;
while ((entry = tarInputStream.getNextEntry()) != null) {
String fileName = entry.getName();
if (!entry.isDirectory() && (isCandidateForSha1(virtualPath + "/" + fileName) || isCandidateForExtract(virtualPath + "/" + fileName))) {
byte[] buffer = new byte[(int) entry.getSize()];
tarInputStream.read(buffer, 0, buffer.length);
FileUtils.writeByteArrayToFile(new File(nestedTempDir +"/" + fileName), buffer);
}
}
} catch (IOException e) {
listener.getLogger().println("Failed to extract archive: ["+tar.getAbsolutePath()+"]: " + e.getMessage());
} finally {
IOUtils.closeQuietly(tarInputStream);
}
return nestedTempDir.exists();
}
示例11: locateArchiveEntry
import org.apache.commons.compress.archivers.ArchiveInputStream; //導入依賴的package包/類
/**
* Searches for an entry inside the zip stream by entry path. If there are alternative extensions, will also look
* for entry with alternative extension. The search stops reading the stream when the entry is found, so calling
* read on the stream will read the returned entry. <p/>
* The zip input stream doesn't support mark/reset so once this method is used you cannot go back - either the
* stream was fully read (when entry is not found) or the stream was read until the current entry.
*
* @param zis The ar input stream
* @param entryPath The entry path to search for
* @param alternativeExtensions List of alternative file extensions to try if the main entry path is not found.
* @return The entry if found, null otherwise
* @throws IOException On failure to read the stream
*/
public static ArchiveEntry locateArchiveEntry(ArchiveInputStream zis, String entryPath,
List<String> alternativeExtensions)
throws IOException {
ArchiveEntry archiveEntry;
while ((archiveEntry = zis.getNextEntry()) != null) {
String zipEntryName = archiveEntry.getName();
if (zipEntryName.equals(entryPath)) {
return archiveEntry;
} else if (alternativeExtensions != null) {
String basePath = PathUtils.stripExtension(entryPath);
for (String alternativeExtension : alternativeExtensions) {
String alternativeSourcePath = basePath + "." + alternativeExtension;
if (zipEntryName.equals(alternativeSourcePath)) {
return archiveEntry;
}
}
}
}
return null;
}
示例12: extractFiles
import org.apache.commons.compress.archivers.ArchiveInputStream; //導入依賴的package包/類
/**
* Extracts the given archive file into the given directory
*
* @param sourceArchive Archive to extract
* @param destinationDirectory Directory to extract archive to
*/
private static void extractFiles(File sourceArchive, File destinationDirectory) {
ArchiveInputStream archiveInputStream = null;
try {
archiveInputStream = createArchiveInputStream(sourceArchive);
ArchiveEntry zipEntry;
while ((zipEntry = archiveInputStream.getNextEntry()) != null) {
//Validate entry name before extracting
String validatedEntryName = validateEntryName(zipEntry.getName());
if (StringUtils.isNotBlank(validatedEntryName)) {
extractFile(sourceArchive, destinationDirectory, archiveInputStream, validatedEntryName,
zipEntry.getLastModifiedDate(), zipEntry.isDirectory());
}
}
} catch (IOException ioe) {
throw new RuntimeException("Error while extracting " + sourceArchive.getPath(), ioe);
} finally {
IOUtils.closeQuietly(archiveInputStream);
}
}
示例13: returnArchiveInputStream
import org.apache.commons.compress.archivers.ArchiveInputStream; //導入依賴的package包/類
/**
* return archive input stream
*
* @param inputStream - file input Stream
* @param archiveSuffix - archive suffix
* @return archive input stream
* @throws IOException
*/
public static ArchiveInputStream returnArchiveInputStream(InputStream inputStream, String archiveSuffix)
throws IOException {
if (isZipFamilyArchive(archiveSuffix)) {
return new ZipArchiveInputStream(inputStream);
}
if (isTarArchive(archiveSuffix)) {
return new TarArchiveInputStream(inputStream);
}
if (isTgzFamilyArchive(archiveSuffix) || isGzCompress(archiveSuffix)) {
return new TarArchiveInputStream(new GzipCompressorInputStream(inputStream));
}
return null;
}
示例14: buildArchiveEntriesTree
import org.apache.commons.compress.archivers.ArchiveInputStream; //導入依賴的package包/類
/**
* build archive entries tree from archive data
* @return archive entries tree instance
*/
private ArchiveEntriesTree buildArchiveEntriesTree() {
ArchiveEntriesTree tree = new ArchiveEntriesTree();
String repoKey = getRepoKey();
ArchiveInputStream archiveInputStream = null;
try {
ArchiveEntry archiveEntry;
// create repo path
RepoPath repositoryPath = InternalRepoPathFactory.create(repoKey, getPath());
archiveInputStream = getRepoService().archiveInputStream(repositoryPath);
while ((archiveEntry = archiveInputStream.getNextEntry()) != null) {
tree.insert(InfoFactoryHolder.get().createArchiveEntry(archiveEntry), repoKey, getPath());
}
} catch (IOException e) {
log.error("Failed to get zip Input Stream: " + e.getMessage());
} finally {
if (archiveInputStream != null) {
IOUtils.closeQuietly(archiveInputStream);
}
return tree;
}
}
示例15: addDicomFiles
import org.apache.commons.compress.archivers.ArchiveInputStream; //導入依賴的package包/類
private static void addDicomFiles(ArchiveInputStream in, ArchiveOutput out, int[] counter) throws Throwable {
ArchiveEntry ae = in.getNextEntry();
while (ae != null) {
if (!ae.isDirectory()) {
String dicomFileName = String.format("%08d.dcm", counter[0] + 1);
File tf = PluginTask.createTemporaryFile();
try {
StreamCopy.copy(in, tf);
if (DicomFileCheck.isDicomFile(tf)) {
out.add(DICOM_MIME_TYPE, dicomFileName, tf);
}
} finally {
PluginTask.deleteTemporaryFile(tf);
}
counter[0] += 1;
}
ae = in.getNextEntry();
}
}