本文整理汇总了Java中java.util.zip.ZipEntry类的典型用法代码示例。如果您正苦于以下问题:Java ZipEntry类的具体用法?Java ZipEntry怎么用?Java ZipEntry使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
ZipEntry类属于java.util.zip包,在下文中一共展示了ZipEntry类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getReader
import java.util.zip.ZipEntry; //导入依赖的package包/类
/**
* This method checks if the given file is a Zip file containing one entry (in case of file
* extension .zip). If this is the case, a reader based on a ZipInputStream for this entry is
* returned. Otherwise, this method checks if the file has the extension .gz. If this applies, a
* gzipped stream reader is returned. Otherwise, this method just returns a BufferedReader for
* the given file (file was not zipped at all).
*/
public static BufferedReader getReader(File file, Charset encoding) throws IOException {
// handle zip files if necessary
if (file.getAbsolutePath().endsWith(".zip")) {
try (ZipFile zipFile = new ZipFile(file)) {
if (zipFile.size() == 0) {
throw new IOException("Input of Zip file failed: the file archive does not contain any entries.");
}
if (zipFile.size() > 1) {
throw new IOException("Input of Zip file failed: the file archive contains more than one entry.");
}
Enumeration<? extends ZipEntry> entries = zipFile.entries();
InputStream zipIn = zipFile.getInputStream(entries.nextElement());
return new BufferedReader(new InputStreamReader(zipIn, encoding));
}
} else if (file.getAbsolutePath().endsWith(".gz")) {
return new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(file)), encoding));
} else {
return new BufferedReader(new InputStreamReader(new FileInputStream(file), encoding));
}
}
示例2: RandomAccessLogReader
import java.util.zip.ZipEntry; //导入依赖的package包/类
/**
* create me based on a (potentially zipped) file
*
* @param file
* @throws Exception
*/
public RandomAccessLogReader(File logFile) throws Exception {
if (logFile.getName().endsWith(".zip")) {
File unzipped = new File(logFile.getParentFile(), "unzipped");
zipFile = new ZipFile(logFile);
Enumeration<? extends ZipEntry> entries = zipFile.entries();
if (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if (!entry.isDirectory()) {
if (!unzipped.isDirectory()) {
unzipped.mkdir();
}
elmLogFile = new File(unzipped, entry.getName());
InputStream in = zipFile.getInputStream(entry);
OutputStream out = new FileOutputStream(elmLogFile);
IOUtils.copy(in, out);
IOUtils.closeQuietly(in);
out.close();
}
}
} else {
elmLogFile = logFile;
}
}
示例3: determineTypesInZipFile
import java.util.zip.ZipEntry; //导入依赖的package包/类
private void determineTypesInZipFile(ZipFullEntry entry) {
InputStream in = entry.getInputStream();
if (in == null) {
Logger.getLogger(PackageContents.class.getName()).log(Level.SEVERE, "No file contents provided for {0}", entry.getName());
} else {
try (ZipInputStream bundle = new ZipInputStream(in)) {
ZipEntry jarEntry;
while ((jarEntry = bundle.getNextEntry()) != null) {
ZipFullEntry jarFullEntry = new ZipFullEntry(bundle, jarEntry, entry.getName().endsWith(".jar"));
observeFileEntry(jarFullEntry);
subfiles.put(entry.getName() + "!" + jarFullEntry.getName(), new FileContents(jarFullEntry, this));
}
} catch (IOException ex) {
Logger.getLogger(PackageContents.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
示例4: unzip
import java.util.zip.ZipEntry; //导入依赖的package包/类
/**
* Unzips a file, placing its contents in the given output location.
*
* @param zipFilePath
* input zip file
* @param outputLocation
* zip file output folder
* @throws IOException
* if there was an error reading the zip file or writing the unzipped data
*/
public static void unzip(final String zipFilePath, final String outputLocation) throws IOException {
// Open the zip file
try (final ZipFile zipFile = new ZipFile(zipFilePath)) {
final Enumeration<? extends ZipEntry> enu = zipFile.entries();
while (enu.hasMoreElements()) {
final ZipEntry zipEntry = enu.nextElement();
final String name = zipEntry.getName();
final File outputFile = new File(outputLocation + separator + name);
if (name.endsWith("/")) {
outputFile.mkdirs();
continue;
}
final File parent = outputFile.getParentFile();
if (parent != null) {
parent.mkdirs();
}
// Extract the file
try (final InputStream inputStream = zipFile.getInputStream(zipEntry);
final FileOutputStream outputStream = new FileOutputStream(outputFile)) {
/*
* The buffer is the max amount of bytes kept in RAM during any given time while
* unzipping. Since most windows disks are aligned to 4096 or 8192, we use a
* multiple of those values for best performance.
*/
final byte[] bytes = new byte[8192];
while (inputStream.available() > 0) {
final int length = inputStream.read(bytes);
outputStream.write(bytes, 0, length);
}
}
}
}
}
示例5: rawData
import java.util.zip.ZipEntry; //导入依赖的package包/类
@Override
public String rawData(String relativePath) throws NullPointerException {
//Log.d(TAG, "Reading file at path: " + relativePath);
try {
ZipEntry zipEntry = zipFile.getEntry(relativePath);
InputStream is = zipFile.getInputStream(zipEntry);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line); //.append('\n');
}
//Log.d(TAG, "Reading data: " + sb.toString());
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
示例6: jarDir
import java.util.zip.ZipEntry; //导入依赖的package包/类
public static void jarDir(File dir, String relativePath, ZipOutputStream zos)
throws IOException {
Preconditions.checkNotNull(relativePath, "relativePath");
Preconditions.checkNotNull(zos, "zos");
// by JAR spec, if there is a manifest, it must be the first entry in the
// ZIP.
File manifestFile = new File(dir, JarFile.MANIFEST_NAME);
ZipEntry manifestEntry = new ZipEntry(JarFile.MANIFEST_NAME);
if (!manifestFile.exists()) {
zos.putNextEntry(manifestEntry);
new Manifest().write(new BufferedOutputStream(zos));
zos.closeEntry();
} else {
copyToZipStream(manifestFile, manifestEntry, zos);
}
zos.closeEntry();
zipDir(dir, relativePath, zos, true);
zos.close();
}
示例7: unZipFile
import java.util.zip.ZipEntry; //导入依赖的package包/类
private static void unZipFile(InputStream source, FileObject projectRoot) throws IOException {
try {
ZipInputStream str = new ZipInputStream(source);
ZipEntry entry;
while ((entry = str.getNextEntry()) != null) {
if (entry.isDirectory()) {
FileUtil.createFolder(projectRoot, entry.getName());
} else {
FileObject fo = FileUtil.createData(projectRoot, entry.getName());
if ("nbproject/project.xml".equals(entry.getName())) {
// Special handling for setting name of Ant-based projects; customize as needed:
filterProjectXML(fo, str, projectRoot.getName());
} else {
writeFile(str, fo);
}
}
}
} finally {
source.close();
}
}
示例8: verwerkManifest
import java.util.zip.ZipEntry; //导入依赖的package包/类
private void verwerkManifest(final Path tmpFolder, final FileTime bestandstijd, final JarOutputStream nieuwArchiefStream) throws MojoExecutionException {
final File archiefBestand = new File(tmpFolder.toFile(), JarFile.MANIFEST_NAME);
getLog().debug("Processing manifest");
if (archiefBestand.exists()) {
try (final FileInputStream fis = new FileInputStream(archiefBestand)) {
Manifest manifest = new Manifest();
if (archiefBestand.exists()) {
manifest.read(fis);
}
ZipEntry manifestFolder = new ZipEntry(META_INF_PATH);
fixeerDatumTijd(manifestFolder, bestandstijd);
nieuwArchiefStream.putNextEntry(manifestFolder);
nieuwArchiefStream.closeEntry();
ZipEntry manifestBestand = new ZipEntry(JarFile.MANIFEST_NAME);
fixeerDatumTijd(manifestBestand, bestandstijd);
nieuwArchiefStream.putNextEntry(manifestBestand);
manifest.write(nieuwArchiefStream);
nieuwArchiefStream.closeEntry();
getLog().debug("manifest processed");
} catch (IOException e) {
throw new MojoExecutionException("Cannot write manifest file", e);
}
}
}
示例9: getInputStream
import java.util.zip.ZipEntry; //导入依赖的package包/类
public static InputStream getInputStream(String fname, JarFile jarFile,
JspCompilationContext ctxt, ErrorDispatcher err)
throws JasperException, IOException {
InputStream in = null;
if (jarFile != null) {
String jarEntryName = fname.substring(1, fname.length());
ZipEntry jarEntry = jarFile.getEntry(jarEntryName);
if (jarEntry == null) {
throw new FileNotFoundException(Localizer.getMessage(
"jsp.error.file.not.found", fname));
}
in = jarFile.getInputStream(jarEntry);
} else {
in = ctxt.getResourceAsStream(fname);
}
if (in == null) {
throw new FileNotFoundException(Localizer.getMessage(
"jsp.error.file.not.found", fname));
}
return in;
}
示例10: copyToZipStream
import java.util.zip.ZipEntry; //导入依赖的package包/类
private static void copyToZipStream(File file, ZipEntry entry,
ZipOutputStream zos) throws IOException {
InputStream is = new FileInputStream(file);
try {
zos.putNextEntry(entry);
byte[] arr = new byte[4096];
int read = is.read(arr);
while (read > -1) {
zos.write(arr, 0, read);
read = is.read(arr);
}
} finally {
try {
is.close();
} finally {
zos.closeEntry();
}
}
}
示例11: loadModuleProperties
import java.util.zip.ZipEntry; //导入依赖的package包/类
private Properties loadModuleProperties(String name, File jarFile) {
try {
ZipFile zipFile = new ZipFile(jarFile);
try {
final String entryName = name + "-classpath.properties";
ZipEntry entry = zipFile.getEntry(entryName);
if (entry == null) {
throw new IllegalStateException("Did not find " + entryName + " in " + jarFile.getAbsolutePath());
}
return GUtil.loadProperties(zipFile.getInputStream(entry));
} finally {
zipFile.close();
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
示例12: loadAndValidate
import java.util.zip.ZipEntry; //导入依赖的package包/类
/**
* Load zip entries from package to HashMap and check if meta-file is present
* @param path path to package
* @throws Exception thrown when meta-file is not present in package
*/
private static void loadAndValidate(String path) throws Exception {
boolean found = false;
zip = new ZipFile(path);
Enumeration<? extends ZipEntry> entries = zip.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if (entry.getName().equals("meta-file") && !entry.isDirectory()) {
found = true;
}
GameLoader.entries.put(entry.getName(), entry);
}
if (!found) {
throw new Exception(String.format("'%s' is not valid game!", path));
}
}
示例13: Dex
import java.util.zip.ZipEntry; //导入依赖的package包/类
/**
* Creates a new dex buffer from the dex file {@code file}.
*/
public Dex(File file) throws IOException {
if (FileUtils.hasArchiveSuffix(file.getName())) {
ZipFile zipFile = new ZipFile(file);
ZipEntry entry = zipFile.getEntry(DexFormat.DEX_IN_JAR_NAME);
if (entry != null) {
loadFrom(zipFile.getInputStream(entry));
zipFile.close();
} else {
throw new DexException("Expected " + DexFormat.DEX_IN_JAR_NAME + " in " + file);
}
} else if (file.getName().endsWith(".dex")) {
loadFrom(new FileInputStream(file));
} else {
throw new DexException("unknown output extension: " + file);
}
}
示例14: call
import java.util.zip.ZipEntry; //导入依赖的package包/类
@Override
protected Void call() throws Exception {
Platform.runLater(this.observableList::clear);
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
SAXParser saxParser = saxParserFactory.newSAXParser();
try (ZipFile zipFile = new ZipFile(this.selectedFile)) {
int workMax = zipFile.size();
long workDone = 0;
for (Enumeration<? extends ZipEntry> e = zipFile.entries(); e.hasMoreElements();) {
ZipEntry zipEntry = e.nextElement();
int position = zipEntry.getName().charAt(zipEntry.getName().length() - 7) - '0';
SyantenAnalyzer analyzer = new SyantenAnalyzer(position);
ParseHandler parseHandler = new ParseHandler(analyzer);
try (InputStream is = zipFile.getInputStream(zipEntry);
GZIPInputStream gzis = new GZIPInputStream(is)) {
saxParser.parse(gzis, parseHandler);
}
ArrayList<MahjongScene> scenes = analyzer.getOriScenes();
workDone++;
Platform.runLater(() -> observableList.addAll(scenes));
updateMessage(workDone + "/" + workMax);
updateProgress(workDone, workMax);
}
}
return null;
}
示例15: writeToJar
import java.util.zip.ZipEntry; //导入依赖的package包/类
/**
*
* Writes a resource content to a jar.
*
* @param res
* @param entryName
* @param jarStream
* @param bufferSize
* @return the number of bytes written to the jar file
* @throws Exception
*/
public static int writeToJar(Resource res, String entryName, JarOutputStream jarStream, int bufferSize)
throws IOException {
byte[] readWriteJarBuffer = new byte[bufferSize];
// remove leading / if present.
if (entryName.charAt(0) == '/')
entryName = entryName.substring(1);
jarStream.putNextEntry(new ZipEntry(entryName));
InputStream entryStream = res.getInputStream();
int numberOfBytes;
// read data into the buffer which is later on written to the jar.
while ((numberOfBytes = entryStream.read(readWriteJarBuffer)) != -1) {
jarStream.write(readWriteJarBuffer, 0, numberOfBytes);
}
return numberOfBytes;
}