本文整理汇总了Java中org.apache.commons.compress.archivers.zip.ZipFile.getEntries方法的典型用法代码示例。如果您正苦于以下问题:Java ZipFile.getEntries方法的具体用法?Java ZipFile.getEntries怎么用?Java ZipFile.getEntries使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.commons.compress.archivers.zip.ZipFile
的用法示例。
在下文中一共展示了ZipFile.getEntries方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: extractZip
import org.apache.commons.compress.archivers.zip.ZipFile; //导入方法依赖的package包/类
private void extractZip(ZipFile zipFile) {
Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
while (entries.hasMoreElements()) {
ZipArchiveEntry entry = entries.nextElement();
String fileName = entry.getName();
File outputFile = new File(config.getExtractionFolder(), fileName);
if (entry.isDirectory()) {
makeDirectory(outputFile);
} else {
createNewFile(outputFile);
try {
InputStream inputStream = zipFile.getInputStream(entry);
extractFile(inputStream, outputFile, fileName);
} catch (IOException e) {
throw new ExtractionException("Error extracting file '" + fileName + "' "
+ "from downloaded file: " + config.getDownloadTarget(), e);
}
}
}
}
示例2: TestBarInstaller
import org.apache.commons.compress.archivers.zip.ZipFile; //导入方法依赖的package包/类
/**
* barファイル内エントリのファイルサイズ上限値を超えた場合に例外が発生すること.
*/
@Test
public void barファイル内エントリのファイルサイズ上限値を超えた場合に例外が発生すること() {
TestBarInstaller testBarInstaller = new TestBarInstaller();
URL fileUrl = ClassLoader.getSystemResource("requestData/barInstall/V1_1_2_bar_minimum.bar");
File file = new File(fileUrl.getPath());
try {
ZipFile zipFile = new ZipFile(file, "UTF-8");
Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
long maxBarEntryFileSize = 0;
while (entries.hasMoreElements()) {
ZipArchiveEntry zae = entries.nextElement();
if (zae.isDirectory()) {
continue;
}
testBarInstaller.checkBarFileEntrySize(zae, zae.getName(), maxBarEntryFileSize);
}
fail("Unexpected exception");
} catch (PersoniumCoreException dce) {
String code = PersoniumCoreException.BarInstall.BAR_FILE_ENTRY_SIZE_TOO_LARGE.getCode();
assertEquals(code, dce.getCode());
} catch (Exception ex) {
fail("Unexpected exception");
}
}
示例3: unZipToFolder
import org.apache.commons.compress.archivers.zip.ZipFile; //导入方法依赖的package包/类
/**
* 把一个ZIP文件解压到一个指定的目录中
* @param zipfilename ZIP文件抽象地址
* @param outputdir 目录绝对地址
*/
public static void unZipToFolder(String zipfilename, String outputdir) throws IOException {
File zipfile = new File(zipfilename);
if (zipfile.exists()) {
outputdir = outputdir + File.separator;
FileUtils.forceMkdir(new File(outputdir));
ZipFile zf = new ZipFile(zipfile, "UTF-8");
Enumeration zipArchiveEntrys = zf.getEntries();
while (zipArchiveEntrys.hasMoreElements()) {
ZipArchiveEntry zipArchiveEntry = (ZipArchiveEntry) zipArchiveEntrys.nextElement();
if (zipArchiveEntry.isDirectory()) {
FileUtils.forceMkdir(new File(outputdir + zipArchiveEntry.getName() + File.separator));
} else {
IOUtils.copy(zf.getInputStream(zipArchiveEntry), FileUtils.openOutputStream(new File(outputdir + zipArchiveEntry.getName())));
}
}
} else {
throw new IOException("指定的解压文件不存在:\t" + zipfilename);
}
}
示例4: getHashesFromZipFile
import org.apache.commons.compress.archivers.zip.ZipFile; //导入方法依赖的package包/类
public List<String> getHashesFromZipFile(File file) throws IOException {
List<String> hashes = new ArrayList<>();
ZipFile zipFile = new ZipFile(file);
byte[] buf = new byte[65536];
Enumeration<?> entries = zipFile.getEntries();
while (entries.hasMoreElements()) {
ZipArchiveEntry zipArchiveEntry = (ZipArchiveEntry) entries.nextElement();
int n;
InputStream is = zipFile.getInputStream(zipArchiveEntry);
ZipArchiveInputStream zis = new ZipArchiveInputStream(is);
if (zis.canReadEntryData(zipArchiveEntry)) {
while ((n = is.read(buf)) != -1) {
if (n > 0) {
hashes.add(DigestUtils.md5Hex(buf));
}
}
}
zis.close();
}
return hashes;
}
示例5: extractZipFile
import org.apache.commons.compress.archivers.zip.ZipFile; //导入方法依赖的package包/类
private static void extractZipFile(final File destination, final ZipFile zipFile) throws IOException {
final Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
while (entries.hasMoreElements()) {
final ZipArchiveEntry entry = entries.nextElement();
final File entryDestination = new File(destination, entry.getName());
if (entry.isDirectory()) {
entryDestination.mkdirs();
} else {
entryDestination.getParentFile().mkdirs();
final InputStream in = zipFile.getInputStream(entry);
try (final OutputStream out = new FileOutputStream(entryDestination)) {
IOUtils.copy(in, out);
IOUtils.closeQuietly(in);
}
}
}
}
示例6: makeTargetZipFile
import org.apache.commons.compress.archivers.zip.ZipFile; //导入方法依赖的package包/类
/**
* Writes a modified version of zip_Source into target.
*
* @author S3460
* @param zipSource the zip source
* @param target the target
* @return the zip file
* @throws Exception the exception
*/
private ZipFile makeTargetZipFile(ZipFile zipSource, File target) throws Exception {
ZipArchiveOutputStream out = new ZipArchiveOutputStream(new FileOutputStream(target));
for (Enumeration<ZipArchiveEntry> enumer = zipSource.getEntries(); enumer.hasMoreElements();) {
ZipArchiveEntry sourceEntry = enumer.nextElement();
out.putArchiveEntry(new ZipArchiveEntry(sourceEntry.getName()));
byte[] oldBytes = toBytes(zipSource, sourceEntry);
byte[] newBytes = getRandomBytes();
byte[] mixedBytes = mixBytes(oldBytes, newBytes);
out.write(mixedBytes, 0, mixedBytes.length);
out.flush();
out.closeArchiveEntry();
}
out.putArchiveEntry(new ZipArchiveEntry("zipentry" + entryMaxSize + 1));
byte[] bytes = getRandomBytes();
out.write(bytes, 0, bytes.length);
out.flush();
out.closeArchiveEntry();
out.putArchiveEntry(new ZipArchiveEntry("zipentry" + (entryMaxSize + 2)));
out.closeArchiveEntry();
out.flush();
out.finish();
out.close();
return new ZipFile(targetFile);
}
示例7: compareFiles
import org.apache.commons.compress.archivers.zip.ZipFile; //导入方法依赖的package包/类
/**
* Compares the content of two zip files. The zip files are considered equal, if
* the content of all zip entries is equal to the content of its corresponding entry
* in the other zip file.
*
* @author S3460
* @param zipSource the zip source
* @param resultZip the result zip
* @throws Exception the exception
*/
private void compareFiles(ZipFile zipSource, ZipFile resultZip) throws Exception {
boolean rc = false;
try {
for (Enumeration<ZipArchiveEntry> enumer = zipSource.getEntries(); enumer.hasMoreElements();) {
ZipArchiveEntry sourceEntry = enumer.nextElement();
ZipArchiveEntry resultEntry = resultZip.getEntry(sourceEntry.getName());
assertNotNull("Entry nicht generiert: " + sourceEntry.getName(), resultEntry);
byte[] oldBytes = toBytes(zipSource, sourceEntry);
byte[] newBytes = toBytes(resultZip, resultEntry);
rc = equal(oldBytes, newBytes);
assertTrue("bytes the same " + sourceEntry, rc);
}
} finally {
zipSource.close();
resultZip.close();
}
}
示例8: detectKmz
import org.apache.commons.compress.archivers.zip.ZipFile; //导入方法依赖的package包/类
private static MediaType detectKmz(ZipFile zip) {
boolean kmlFound = false;
Enumeration<ZipArchiveEntry> entries = zip.getEntries();
while (entries.hasMoreElements()) {
ZipArchiveEntry entry = entries.nextElement();
String name = entry.getName();
if (!entry.isDirectory()
&& name.indexOf('/') == -1 && name.indexOf('\\') == -1) {
if (name.endsWith(".kml") && !kmlFound) {
kmlFound = true;
} else {
return null;
}
}
}
if (kmlFound) {
return MediaType.application("vnd.google-earth.kmz");
} else {
return null;
}
}
示例9: detectIpa
import org.apache.commons.compress.archivers.zip.ZipFile; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
private static MediaType detectIpa(ZipFile zip) {
// Note - consider generalising this logic, if another format needs many regexp matching
Set<Pattern> tmpPatterns = (Set<Pattern>)ipaEntryPatterns.clone();
Enumeration<ZipArchiveEntry> entries = zip.getEntries();
while (entries.hasMoreElements()) {
ZipArchiveEntry entry = entries.nextElement();
String name = entry.getName();
Iterator<Pattern> ip = tmpPatterns.iterator();
while (ip.hasNext()) {
if (ip.next().matcher(name).matches()) {
ip.remove();
}
}
if (tmpPatterns.isEmpty()) {
// We've found everything we need to find
return MediaType.application("x-itunes-ipa");
}
}
// If we get here, not all required entries were found
return null;
}
示例10: testEmptyBaseDirRelativeDir
import org.apache.commons.compress.archivers.zip.ZipFile; //导入方法依赖的package包/类
@Test
public void testEmptyBaseDirRelativeDir() throws Exception {
String aTargetFilename = "target/Z6-input.zip";
ZipFilesTasklet aTasklet = new ZipFilesTasklet();
aTasklet.setSourceBaseDirectory(new FileSystemResource(""));
FileSystemResourcesFactory aSourceFactory = new FileSystemResourcesFactory();
aSourceFactory.setPattern("file:src/test/resources/testFiles/input.csv");
aTasklet.setSourceFactory(aSourceFactory );
ExpressionResourceFactory aDestinationFactory = new ExpressionResourceFactory();
aDestinationFactory.setExpression(aTargetFilename);
aTasklet.setDestinationFactory(aDestinationFactory );
assertEquals(RepeatStatus.FINISHED, aTasklet.execute(null, null));
assertTrue(new File(aTargetFilename).exists());
ZipFile aZipFile = new ZipFile(new File(aTargetFilename));
Enumeration<ZipArchiveEntry> aEntries = aZipFile.getEntries();
assertTrue(aEntries.hasMoreElements());
assertEquals("src/test/resources/testFiles/input.csv", aEntries.nextElement().getName());
assertFalse(aEntries.hasMoreElements());
aZipFile.close();
}
示例11: readFirstZipEntry
import org.apache.commons.compress.archivers.zip.ZipFile; //导入方法依赖的package包/类
/**
* Reads the first file entry in a zip file and returns it's contents
* as uncompressed byte-array
* @param zipFile the zip file to read from
* @return the first file entry (uncompressed)
* @throws IOException if there is an error accessing the zip file
*/
public static byte[] readFirstZipEntry(File zipFile) throws IOException {
// open zip
ZipFile zf = new ZipFile(zipFile);
Enumeration<ZipArchiveEntry> entries = zf.getEntries();
// read first entry to byte[]
ZipArchiveEntry entry = entries.nextElement();
InputStream is = zf.getInputStream(entry);
byte[] raw = ByteStreams.toByteArray(is);
// close all streams and return byte[]
is.close();
zf.close();
return raw;
}
示例12: unzip
import org.apache.commons.compress.archivers.zip.ZipFile; //导入方法依赖的package包/类
public static void unzip(File zipFile, File destination)
throws IOException {
ZipFile zip = new ZipFile(zipFile);
try {
Enumeration<ZipArchiveEntry> e = zip.getEntries();
while (e.hasMoreElements()) {
ZipArchiveEntry entry = e.nextElement();
File file = new File(destination, entry.getName());
if (entry.isDirectory()) {
file.mkdirs();
} else {
InputStream is = zip.getInputStream(entry);
File parent = file.getParentFile();
if (parent != null && parent.exists() == false) {
parent.mkdirs();
}
FileOutputStream os = new FileOutputStream(file);
try {
IOUtils.copy(is, os);
} finally {
os.close();
is.close();
}
file.setLastModified(entry.getTime());
int mode = entry.getUnixMode();
if ((mode & EXEC_MASK) != 0) {
if (!file.setExecutable(true)) {
}
}
}
}
} finally {
ZipFile.closeQuietly(zip);
}
}
示例13: unzipToTemporaryDirectory
import org.apache.commons.compress.archivers.zip.ZipFile; //导入方法依赖的package包/类
public static Path unzipToTemporaryDirectory(File file) throws IOException {
ZipFile zipFile = new ZipFile(file);
Enumeration<ZipArchiveEntry> zipEntries = zipFile.getEntries();
Path tempDirectory = Files.createTempDirectory("temp");
while (zipEntries.hasMoreElements()) {
ZipArchiveEntry entry = zipEntries.nextElement();
FileUtils.copyInputStreamToFile(zipFile.getInputStream(entry), new File(Paths.get(tempDirectory.toString(),"/" + entry.getName()).toString()));
}
zipFile.close();
return tempDirectory;
}
示例14: extractAll
import org.apache.commons.compress.archivers.zip.ZipFile; //导入方法依赖的package包/类
public static void extractAll(String zipFile, String extractDir) throws Exception {
ZipFile unzipFile = new ZipFile(zipFile);
try {
File root = new File(extractDir);
Enumeration<ZipArchiveEntry> fileHeaderList = unzipFile.getEntries();
while(fileHeaderList.hasMoreElements()) {
ZipArchiveEntry fileHeader = fileHeaderList.nextElement();
if (fileHeader.isDirectory()) {
//...
} else if (!fileHeader.isUnixSymlink()) {
File f = new File(root, fileHeader.getName());
File dir = f.getParentFile();
if (!dir.exists()) {
dir.mkdirs();
}
FileOutputStream fout = new FileOutputStream(f);
try {
IOUtils.copy(unzipFile.getInputStream(fileHeader), fout);
} finally {
try {
fout.close();
} catch (Throwable e) {
}
}
f.setLastModified(fileHeader.getLastModifiedDate().getTime());
}
}
} finally {
ZipFile.closeQuietly(unzipFile);
}
}
示例15: decompressZip
import org.apache.commons.compress.archivers.zip.ZipFile; //导入方法依赖的package包/类
public void decompressZip(File zipFile, String dir) throws IOException {
ZipFile zf = new ZipFile(zipFile);
try {
for (Enumeration<ZipArchiveEntry> entries = zf.getEntries(); entries
.hasMoreElements();) {
ZipArchiveEntry ze = entries.nextElement();
// 不存在则创建目标文件夹。
File targetFile = new File(dir, ze.getName());
// 遇到根目录时跳过。
if (ze.getName().lastIndexOf("/") == (ze.getName().length() - 1)) {
continue;
}
// 如果文件夹不存在,创建文件夹。
if (!targetFile.getParentFile().exists()) {
targetFile.getParentFile().mkdirs();
}
InputStream i = zf.getInputStream(ze);
OutputStream o = null;
try {
o = new FileOutputStream(targetFile);
IOUtils.copy(i, o);
} finally {
if (i != null) {
i.close();
}
if (o != null) {
o.close();
}
}
}
} finally {
zf.close();
}
}