本文整理了Java中java.util.zip.ZipFile
类的一些代码示例,展示了ZipFile
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ZipFile
类的具体详情如下:
包路径:java.util.zip.ZipFile
类名称:ZipFile
[英]This class provides random read access to a zip file. You pay more to read the zip file's central directory up front (from the constructor), but if you're using #getEntry to look up multiple files by name, you get the benefit of this index.
If you only want to iterate through all the files (using #entries(), you should consider ZipInputStream, which provides stream-like read access to a zip file and has a lower up-front cost because you don't pay to build an in-memory index.
If you want to create a zip file, use ZipOutputStream. There is no API for updating an existing zip file.
[中]此类提供对zip文件的随机读取访问。提前读取zip文件的中心目录(从构造函数中)需要花费更多的钱,但如果使用#getEntry按名称查找多个文件,则可以使用此索引。
如果您只想遍历所有文件(使用μi),则应该考虑zIPunPoSt流,它为zip文件提供流式读取访问,并且具有较低的前置成本,因为您不必支付内存索引。
如果要创建zip文件,请使用ZipOutStream。没有用于更新现有zip文件的API。
代码示例来源:origin: skylot/jadx
private static List<String> getZipFileList(File file) {
List<String> filesList = new ArrayList<>();
try (ZipFile zipFile = new ZipFile(file)) {
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
filesList.add(entry.getName());
}
} catch (Exception e) {
LOG.error("Error read zip file '{}'", file.getAbsolutePath(), e);
}
return filesList;
}
代码示例来源:origin: Tencent/tinker
private static ByteBuffer getZipEntryData(ZipFile zf, String entryPath) throws IOException {
final ZipEntry entry = zf.getEntry(entryPath);
InputStream is = null;
try {
is = new BufferedInputStream(zf.getInputStream(entry));
final byte[] data = Utils.toByteArray(is);
return ByteBuffer.wrap(data);
} finally {
if (is != null) {
try {
is.close();
} catch (Throwable ignored) {
// Ignored.
}
}
}
}
代码示例来源:origin: Blankj/AndroidUtilCode
/**
* Return the files' comment in ZIP file.
*
* @param zipFile The ZIP file.
* @return the files' comment in ZIP file
* @throws IOException if an I/O error has occurred
*/
public static List<String> getComments(final File zipFile)
throws IOException {
if (zipFile == null) return null;
List<String> comments = new ArrayList<>();
ZipFile zip = new ZipFile(zipFile);
Enumeration<?> entries = zip.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = ((ZipEntry) entries.nextElement());
comments.add(entry.getComment());
}
zip.close();
return comments;
}
代码示例来源:origin: libgdx/libgdx
private InputStream getFromJar (String jarFile, String sharedLibrary) throws IOException {
ZipFile file = new ZipFile(nativesJar);
ZipEntry entry = file.getEntry(sharedLibrary);
return file.getInputStream(entry);
}
代码示例来源:origin: skylot/jadx
private static boolean isZipFileCanBeOpen(File file) {
try (ZipFile zipFile = new ZipFile(file)) {
return zipFile.entries().hasMoreElements();
} catch (Exception e) {
return false;
}
}
代码示例来源:origin: pxb1988/dex2jar
@Override
public InputStream _newInputStream() throws IOException {
ZipEntry e = zipFile.getEntry(path);
return e != null ? zipFile.getInputStream(e) : null;
}
}
代码示例来源:origin: twosigma/beakerx
private static void unzipRepo() {
try {
ZipFile zipFile = new ZipFile(BUILD_PATH + "/testMvnCache.zip");
Enumeration<?> enu = zipFile.entries();
while (enu.hasMoreElements()) {
ZipEntry zipEntry = (ZipEntry) enu.nextElement();
String name = BUILD_PATH + "/" + zipEntry.getName();
File file = new File(name);
if (name.endsWith("/")) {
InputStream is = zipFile.getInputStream(zipEntry);
FileOutputStream fos = new FileOutputStream(file);
byte[] bytes = new byte[1024];
int length;
while ((length = is.read(bytes)) >= 0) {
fos.write(bytes, 0, length);
is.close();
fos.close();
zipFile.close();
} catch (IOException e) {
throw new RuntimeException(e);
代码示例来源:origin: stackoverflow.com
OutputStream output = new FileOutputStream(StorezipFileLocation);
while ((count = input.read(data)) != -1)
input.close();
result = "true";
try
ZipFile zipfile = new ZipFile(archive);
for (Enumeration e = zipfile.entries(); e.hasMoreElements();)
ZipEntry entry = (ZipEntry) e.nextElement();
unzipEntry(zipfile, entry, destinationPath);
if (entry.isDirectory())
createDir(new File(outputDir, entry.getName()));
return;
File outputFile = new File(outputDir, entry.getName());
if (!outputFile.getParentFile().exists())
BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry));
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
代码示例来源:origin: geoserver/geoserver
public static void decompress(final File inputFile, final File destDir) throws IOException {
try (ZipFile zipFile = new ZipFile(inputFile)) {
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
File newFile = getZipOutputFile(destDir, entry);
if (entry.isDirectory()) {
// Assume directories are stored parents first then children.
newFile.mkdir();
continue;
}
InputStream stream = zipFile.getInputStream(entry);
FileOutputStream fos = new FileOutputStream(newFile);
try {
byte[] buf = new byte[1024];
int len;
while ((len = stream.read(buf)) >= 0) saveCompressedStream(buf, fos, len);
} catch (IOException e) {
IOException ioe = new IOException("Not valid archive file type.");
ioe.initCause(e);
throw ioe;
} finally {
fos.flush();
fos.close();
stream.close();
}
}
}
}
代码示例来源:origin: deeplearning4j/nd4j
file.deleteOnExit();
ZipFile zipFile = new ZipFile(url.getFile());
ZipEntry entry = zipFile.getEntry(this.resourceName);
if (entry == null) {
if (this.resourceName.startsWith("/")) {
entry = zipFile.getEntry(this.resourceName.replaceFirst("/", ""));
if (entry == null) {
throw new FileNotFoundException("Resource " + this.resourceName + " not found");
long size = entry.getSize();
InputStream stream = zipFile.getInputStream(entry);
FileOutputStream outputStream = new FileOutputStream(file);
byte[] array = new byte[1024];
int rd = 0;
long bytesRead = 0;
do {
rd = stream.read(array);
outputStream.write(array, 0, rd);
bytesRead += rd;
} while (bytesRead < size);
outputStream.flush();
outputStream.close();
stream.close();
zipFile.close();
代码示例来源:origin: JZ-Darkal/AndroidHttpCapture
ZipFile zipFile = null;
try {
zipFile = new ZipFile(filePath);
for (Enumeration entry = zipFile.entries(); entry.hasMoreElements(); ) {
ZipEntry zipEntry = (ZipEntry) entry.nextElement();
if (zipEntry.isDirectory()) {
if (zipEntry.getSize() > 0) {
File file = FileUtil.getFileByPath(unzipPath + "/" + zipEntry.getName(), false);
OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
InputStream is = zipFile.getInputStream(zipEntry);
byte[] buffer = new byte[4096];
int len = 0;
while ((len = is.read(buffer)) >= 0) {
os.write(buffer, 0, len);
zipFile.close();
} catch (Exception e) {
e.printStackTrace();
if (zipFile != null) {
try {
zipFile.close();
} catch (IOException e) {
e.printStackTrace();
代码示例来源:origin: Tencent/tinker
checkDirectory(filePath);
ZipFile zipFile = new ZipFile(fileName);
Enumeration enumeration = zipFile.entries();
try {
while (enumeration.hasMoreElements()) {
ZipEntry entry = (ZipEntry) enumeration.nextElement();
if (entry.isDirectory()) {
new File(filePath, entry.getName()).mkdirs();
continue;
BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(entry));
File file = new File(filePath + File.separator + entry.getName());
BufferedOutputStream bos = null;
try {
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos, TypedValue.BUFFER_SIZE);
fos.write(buf, 0, len);
bos.flush();
bos.close();
zipFile.close();
代码示例来源:origin: Sable/soot
private void copyAllButClassesDexAndSigFiles(ZipFile source, ZipOutputStream destination) throws IOException {
Enumeration<? extends ZipEntry> sourceEntries = source.entries();
while (sourceEntries.hasMoreElements()) {
ZipEntry sourceEntry = sourceEntries.nextElement();
String sourceEntryName = sourceEntry.getName();
if (sourceEntryName.endsWith(".dex") || isSignatureFile(sourceEntryName)) {
continue;
}
// separate ZipEntry avoids compression problems due to encodings
ZipEntry destinationEntry = new ZipEntry(sourceEntryName);
// use the same compression method as the original (certain files
// are stored, not compressed)
destinationEntry.setMethod(sourceEntry.getMethod());
// copy other necessary fields for STORE method
destinationEntry.setSize(sourceEntry.getSize());
destinationEntry.setCrc(sourceEntry.getCrc());
// finally craft new entry
destination.putNextEntry(destinationEntry);
InputStream zipEntryInput = source.getInputStream(sourceEntry);
byte[] buffer = new byte[2048];
int bytesRead = zipEntryInput.read(buffer);
while (bytesRead > 0) {
destination.write(buffer, 0, bytesRead);
bytesRead = zipEntryInput.read(buffer);
}
zipEntryInput.close();
}
}
代码示例来源:origin: Blankj/AndroidUtilCode
File file = new File(destDir, name);
files.add(file);
if (entry.isDirectory()) {
return createOrExistsDir(file);
} else {
OutputStream out = null;
try {
in = new BufferedInputStream(zip.getInputStream(entry));
out = new BufferedOutputStream(new FileOutputStream(file));
byte buffer[] = new byte[BUFFER_LEN];
int len;
while ((len = in.read(buffer)) != -1) {
out.write(buffer, 0, len);
in.close();
代码示例来源:origin: h2oai/h2o-2
/** Extracts the libraries from the jar file to given local path. */
private void extractInternalFiles() throws IOException {
Enumeration entries = _h2oJar.entries();
while( entries.hasMoreElements() ) {
ZipEntry e = (ZipEntry) entries.nextElement();
String name = e.getName();
if( e.isDirectory() ) continue; // mkdirs() will handle these
if(! name.endsWith(".jar") ) continue;
// extract the entry
File out = internalFile(name);
out.getParentFile().mkdirs();
try {
FileOutputStream fos = new FileOutputStream(out);
BufferedInputStream is = new BufferedInputStream (_h2oJar.getInputStream(e));
BufferedOutputStream os = new BufferedOutputStream(fos);
int read;
byte[] buffer = new byte[4096];
while( (read = is.read(buffer)) != -1 ) os.write(buffer,0,read);
os.flush();
fos.getFD().sync(); // Force the output; throws SyncFailedException if full
os.close();
is.close();
} catch( FileNotFoundException ex ) {
// Expected FNF if 2 H2O instances are attempting to unpack in the same directory
} catch( IOException ex ) {
Log.die("Unable to extract file "+name+" because of "+ex+". Make sure that directory " + _parentDir + " contains at least 50MB of free space to unpack H2O libraries.");
throw ex; // dead code
}
}
}
代码示例来源:origin: stackoverflow.com
source = domain.getCodeSource();
url = source.getLocation();
uri = url.toURI();
zipFile = new ZipFile(location);
zipFile.close();
entry = zipFile.getEntry(fileName);
throw new FileNotFoundException("cannot find file: " + fileName + " in archive: " + zipFile.getName());
zipStream = zipFile.getInputStream(entry);
fileStream = null;
int i;
fileStream = new FileOutputStream(tempFile);
buf = new byte[1024];
i = 0;
while((i = zipStream.read(buf)) != -1)
代码示例来源:origin: stackoverflow.com
final String path = "sample/folder";
final File jarFile = new File(getClass().getProtectionDomain().getCodeSource().getLocation().getPath());
if(jarFile.isFile()) { // Run with JAR file
final JarFile jar = new JarFile(jarFile);
final Enumeration<JarEntry> entries = jar.entries(); //gives ALL entries in jar
while(entries.hasMoreElements()) {
final String name = entries.nextElement().getName();
if (name.startsWith(path + "/")) { //filter according to the path
System.out.println(name);
}
}
jar.close();
} else { // Run with IDE
final URL url = Launcher.class.getResource("/" + path);
if (url != null) {
try {
final File apps = new File(url.toURI());
for (File app : apps.listFiles()) {
System.out.println(app);
}
} catch (URISyntaxException ex) {
// never happens
}
}
}
代码示例来源:origin: marytts/marytts
private static void unzipEntry(ZipFile zipfile, ZipEntry entry, File outputDir) throws IOException {
if (entry.isDirectory()) {
createDir(new File(outputDir, entry.getName()));
return;
}
File outputFile = new File(outputDir, entry.getName());
if (!outputFile.getParentFile().exists()) {
createDir(outputFile.getParentFile());
}
BufferedInputStream inputStream = new BufferedInputStream(zipfile.getInputStream(entry));
BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(outputFile));
try {
IOUtils.copy(inputStream, outputStream);
} finally {
outputStream.close();
inputStream.close();
}
}
代码示例来源:origin: Tencent/tinker
is = new BufferedInputStream(zipFile.getInputStream(entryFile));
os = new BufferedOutputStream(new FileOutputStream(extractTo));
byte[] buffer = new byte[ShareConstants.BUFFER_SIZE];
int length = 0;
while ((length = is.read(buffer)) > 0) {
os.write(buffer, 0, length);
代码示例来源:origin: Tencent/tinker
try {
zos = new ZipOutputStream(new
BufferedOutputStream(new FileOutputStream(extractTo)));
bis = new BufferedInputStream(zipFile.getInputStream(entryFile));
ZipEntry entry = new ZipEntry(ShareConstants.DEX_IN_JAR);
zos.putNextEntry(entry);
int length = bis.read(buffer);
内容来源于网络,如有侵权,请联系作者删除!