本文整理匯總了Java中org.apache.commons.compress.archivers.zip.ZipFile類的典型用法代碼示例。如果您正苦於以下問題:Java ZipFile類的具體用法?Java ZipFile怎麽用?Java ZipFile使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
ZipFile類屬於org.apache.commons.compress.archivers.zip包,在下文中一共展示了ZipFile類的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: downloadZipAndExtract
import org.apache.commons.compress.archivers.zip.ZipFile; //導入依賴的package包/類
protected static void downloadZipAndExtract(final String url, final File targetDirectory) {
download(url, inputStream -> {
try {
final byte[] bytes = IOUtils.toByteArray(inputStream);
try (final ZipFile zipFile = new ZipFile(new SeekableInMemoryByteChannel(bytes))) {
final Enumeration<ZipArchiveEntry> entries = zipFile.getEntries();
while (entries.hasMoreElements()) {
final ZipArchiveEntry entry = entries.nextElement();
final File file = new File(targetDirectory, entry.getName());
if (entry.isDirectory()) {
file.mkdir();
continue;
}
try (final OutputStream outputStream =
new BufferedOutputStream(new FileOutputStream(file))) {
IOUtils.copy(zipFile.getInputStream(entry), outputStream);
}
}
}
} catch (final IOException exception) {
throw new RuntimeException(exception);
}
});
}
示例3: 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");
}
}
示例4: handle
import org.apache.commons.compress.archivers.zip.ZipFile; //導入依賴的package包/類
@Override
public List<ProcessTask> handle(EntityManager manager) throws Exception {
if (!file.exists()) {
return todo;
}
try (ZipFile zip = new ZipFile(file)) {
for (ZipArchiveEntry entry : new IteratorIterable<>(new EnumerationIterator<>(zip.getEntries()))) {
try {
handleSingleZipEntry(zip, entry, manager);
} catch (IOException ex) {
System.out.println(ex.getLocalizedMessage());
}
}
}
return todo;
}
示例5: unZip
import org.apache.commons.compress.archivers.zip.ZipFile; //導入依賴的package包/類
public static void unZip(Path file, Path target) throws IOException {
try (final ZipFile zipFile = new ZipFile(file.toFile())) {
final Enumeration<ZipArchiveEntry> files = zipFile.getEntriesInPhysicalOrder();
while (files.hasMoreElements()) {
final ZipArchiveEntry entry = files.nextElement();
final Path entryFile = target.resolve(REMOVE_LEADING_GO_PATH_PATTERN.matcher(entry.getName()).replaceFirst("")).toAbsolutePath();
if (entry.isDirectory()) {
createDirectoriesIfRequired(entryFile);
} else {
ensureParentOf(entryFile);
try (final InputStream is = zipFile.getInputStream(entry)) {
try (final OutputStream os = newOutputStream(entryFile)) {
copy(is, os);
}
}
}
}
}
}
示例6: 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);
}
}
示例7: extractParsecRdlGenerator
import org.apache.commons.compress.archivers.zip.ZipFile; //導入依賴的package包/類
/**
* Extract Parsec Rdl Generator.
*
* @param rdlBinSuffix rdl bin suffix
* @param generators the generator list
* @throws IOException IOException
*/
void extractParsecRdlGenerator(final String rdlBinSuffix, final List<String> generators) throws IOException {
final File file = fileUtils.getFileFromResource("/rdl-gen/rdl-gen.zip");
for (String g: generators) {
String generatorBinary = PathUtils.RDL_GEN_PARSEC_PREFIX + g;
try (
ZipFile zipFile = new ZipFile(file);
InputStream inputStream = zipFile.getInputStream(
zipFile.getEntry(generatorBinary + "-" + rdlBinSuffix)
)
) {
fileUtils.writeResourceAsExecutable(inputStream, pathUtils.getBinPath() + "/" + generatorBinary);
}
}
}
示例8: 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;
}
示例9: extract
import org.apache.commons.compress.archivers.zip.ZipFile; //導入依賴的package包/類
private static void extract(ZipArchiveEntry zipArchiveEntry, ZipFile zipFile, File outputDir) throws IOException {
File extractedFile = new File(outputDir, zipArchiveEntry.getName());
FileUtils.forceMkdir(extractedFile.getParentFile());
if (zipArchiveEntry.isUnixSymlink()) {
if (PosixUtil.isPosixFileStore(outputDir)) {
logger.debug("Extracting [l] "+zipArchiveEntry.getName());
String symlinkTarget = zipFile.getUnixSymlink(zipArchiveEntry);
Files.createSymbolicLink(extractedFile.toPath(), new File(symlinkTarget).toPath());
} else {
logger.debug("Skipping ! [l] "+zipArchiveEntry.getName());
}
} else if (zipArchiveEntry.isDirectory()) {
logger.debug("Extracting [d] "+zipArchiveEntry.getName());
FileUtils.forceMkdir(extractedFile);
} else {
logger.debug("Extracting [f] "+zipArchiveEntry.getName());
try (
InputStream in = zipFile.getInputStream(zipArchiveEntry);
OutputStream out = new FileOutputStream(extractedFile)
) {
IOUtils.copy(in, out);
}
}
updatePermissions(extractedFile, zipArchiveEntry.getUnixMode());
}
示例10: getZipArchiveEntryNames
import org.apache.commons.compress.archivers.zip.ZipFile; //導入依賴的package包/類
private static List<String> getZipArchiveEntryNames(File sourceZip) throws IOException {
List<String> content = new ArrayList<>();
try (ZipFile zipFile = new ZipFile(sourceZip);) {
List<ZipArchiveEntry> zipEntries = Collections.list(zipFile.getEntries());
for (ZipArchiveEntry zipEntry : zipEntries) {
if (zipEntry.isUnixSymlink()) {
content.add("[l]"+zipEntry.getName());
} else if (zipEntry.isDirectory()) {
content.add("[d]"+zipEntry.getName());
} else {
content.add("[f]"+zipEntry.getName());
}
}
}
Collections.sort(content);
return content;
}
示例11: 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);
}
}
}
}
示例12: makeSourceZipFile
import org.apache.commons.compress.archivers.zip.ZipFile; //導入依賴的package包/類
/**
* Creates a zip file with random content.
*
* @author S3460
* @param source the source
* @return the zip file
* @throws Exception the exception
*/
private ZipFile makeSourceZipFile(File source) throws Exception {
ZipArchiveOutputStream out = new ZipArchiveOutputStream(new FileOutputStream(source));
int size = randomSize(entryMaxSize);
for (int i = 0; i < size; i++) {
out.putArchiveEntry(new ZipArchiveEntry("zipentry" + i));
int anz = randomSize(10);
for (int j = 0; j < anz; j++) {
byte[] bytes = getRandomBytes();
out.write(bytes, 0, bytes.length);
}
out.flush();
out.closeArchiveEntry();
}
//add leeres Entry
out.putArchiveEntry(new ZipArchiveEntry("zipentry" + size));
out.flush();
out.closeArchiveEntry();
out.flush();
out.finish();
out.close();
return new ZipFile(source);
}
示例13: 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);
}
示例14: 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();
}
}
示例15: runJarPatcher
import org.apache.commons.compress.archivers.zip.ZipFile; //導入依賴的package包/類
/**
* Run jar patcher.
*
* @param originalName the original name
* @param targetName the target name
* @param originalZip the original zip
* @param newZip the new zip
* @param comparefiles the comparefiles
* @throws Exception the exception
*/
private void runJarPatcher(String originalName, String targetName, ZipFile originalZip, ZipFile newZip, boolean comparefiles) throws Exception {
try (ZipArchiveOutputStream output = new ZipArchiveOutputStream(new FileOutputStream(patchFile))) {
new JarDelta().computeDelta(originalName, targetName, originalZip, newZip, output);
}
ZipFile patch = new ZipFile(patchFile);
ZipArchiveEntry listEntry = patch.getEntry("META-INF/file.list");
if (listEntry == null) {
patch.close();
throw new IOException("Invalid patch - list entry 'META-INF/file.list' not found");
}
BufferedReader patchlist = new BufferedReader(new InputStreamReader(patch.getInputStream(listEntry)));
String next = patchlist.readLine();
String sourceName = next;
next = patchlist.readLine();
new JarPatcher(patchFile.getName(), sourceName).applyDelta(patch, new ZipFile(originalName), new ZipArchiveOutputStream(new FileOutputStream(resultFile)), patchlist);
if (comparefiles) {
compareFiles(new ZipFile(targetName), new ZipFile(resultFile));
}
}