本文整理汇总了Java中org.apache.tools.zip.ZipFile类的典型用法代码示例。如果您正苦于以下问题:Java ZipFile类的具体用法?Java ZipFile怎么用?Java ZipFile使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ZipFile类属于org.apache.tools.zip包,在下文中一共展示了ZipFile类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: ZipExtractor
import org.apache.tools.zip.ZipFile; //导入依赖的package包/类
/**
* コンストラクタ.
* @param filePath 対象ファイルパス.
* @param encoding エンコーディング
*/
public ZipExtractor(String filePath, String encoding) {
// filePathがnullであればNullPointerExceptionが発生
File f = new File(filePath);
// ファイルとして存在していなければ例外throw
if (!f.exists() || !f.isFile()) {
throw new IllegalArgumentException(
String.format("Zip file[%s] is not found, or is not file.", filePath));
}
try {
this.encoding = encoding;
this.zipFile = new ZipFile(f, encoding);
} catch (IOException e) {
throw new ZipUtilRuntimeException(e);
}
}
示例2: isFileConflict
import org.apache.tools.zip.ZipFile; //导入依赖的package包/类
@SuppressWarnings("unchecked")
private boolean isFileConflict(File file) throws IOException{
ZipFile zip = new ZipFile(file, "GBK");
ZipEntry entry;
String name;
String filename;
File outFile;
boolean fileConflict=false;
Enumeration<ZipEntry> en = zip.getEntries();
while (en.hasMoreElements()) {
entry = en.nextElement();
name = entry.getName();
if (!entry.isDirectory()) {
name = entry.getName();
filename = name;
outFile = new File(realPathResolver.get(filename));
if(outFile.exists()){
fileConflict=true;
break;
}
}
}
zip.close();
return fileConflict;
}
示例3: getPlugPerms
import org.apache.tools.zip.ZipFile; //导入依赖的package包/类
@SuppressWarnings("unchecked")
private String getPlugPerms(File file) throws IOException{
ZipFile zip = new ZipFile(file, "GBK");
ZipEntry entry;
String name,filename;
File propertyFile;
String plugPerms="";
Enumeration<ZipEntry> en = zip.getEntries();
while (en.hasMoreElements()) {
entry = en.nextElement();
name = entry.getName();
if (!entry.isDirectory()) {
name = entry.getName();
filename = name;
//读取属性文件的plug.mark属性
if(filename.startsWith(PLUG_FILE_PREFIX)&&filename.endsWith(".properties")){
propertyFile = new File(realPathResolver.get(filename));
Properties p=new Properties();
p.load(new FileInputStream(propertyFile));
plugPerms=p.getProperty(PLUG_PERMS);
}
}
}
zip.close();
return plugPerms;
}
示例4: getInputStream
import org.apache.tools.zip.ZipFile; //导入依赖的package包/类
/**
* Return an InputStream for reading the contents of this Resource.
* @return an InputStream object.
* @throws IOException if the zip file cannot be opened,
* or the entry cannot be read.
*/
public InputStream getInputStream() throws IOException {
if (isReference()) {
return ((Resource) getCheckedRef()).getInputStream();
}
final ZipFile z = new ZipFile(getZipfile(), getEncoding());
ZipEntry ze = z.getEntry(getName());
if (ze == null) {
z.close();
throw new BuildException("no entry " + getName() + " in "
+ getArchive());
}
return new FilterInputStream(z.getInputStream(ze)) {
public void close() throws IOException {
FileUtils.close(in);
z.close();
}
protected void finalize() throws Throwable {
try {
close();
} finally {
super.finalize();
}
}
};
}
示例5: getUnixMode
import org.apache.tools.zip.ZipFile; //导入依赖的package包/类
/**
* Determine a Resource's Unix mode or return the given default
* value if not available.
*/
private int getUnixMode(final Resource r, final ZipFile zf, final int defaultMode) {
int unixMode = defaultMode;
if (zf != null) {
final ZipEntry ze = zf.getEntry(r.getName());
unixMode = ze.getUnixMode();
if ((unixMode == 0 || unixMode == UnixStat.DIR_FLAG)
&& !preserve0Permissions) {
unixMode = defaultMode;
}
} else if (r instanceof ArchiveResource) {
unixMode = ((ArchiveResource) r).getMode();
}
return unixMode;
}
示例6: unzipToDirectory
import org.apache.tools.zip.ZipFile; //导入依赖的package包/类
private static void unzipToDirectory(ZipFile zipFile, File destDir) throws IOException {
byte[] buffer = new byte[4096];
Enumeration entries = zipFile.getEntries();
while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();
if (entry.isDirectory()) {
File dir = new File(destDir, entry.getName());
InstallUtils.createDirectory(dir);
} else {
File file = new File(destDir, entry.getName());
InstallUtils.createDirectory(file.getParentFile());
OutputStream out = null;
InputStream in = null;
try {
out = new BufferedOutputStream(new FileOutputStream(file));
in = zipFile.getInputStream(entry);
Unzip.copy(in, out, buffer);
} finally {
InstallUtils.close(in);
InstallUtils.close(out);
}
Unzip.setFilePermissions(file, entry);
}
}
}
示例7: unZipFile
import org.apache.tools.zip.ZipFile; //导入依赖的package包/类
public static void unZipFile(String archive, String decompressDir) throws IOException, FileNotFoundException, ZipException{
BufferedInputStream bufIn = null;
ZipFile zipFile = new ZipFile(archive, "GBK");
Enumeration<?> enumeration = zipFile.getEntries();
while (enumeration.hasMoreElements()) {
ZipEntry zipEntry = (ZipEntry) enumeration.nextElement();
String entryName = zipEntry.getName();
String entryPath = decompressDir +"/" + entryName;
if(zipEntry.isDirectory()){
System.out.println("Decompressing directory -- " + entryName);
File decompDirFile = new File(entryPath);
if(!decompDirFile.exists()){
decompDirFile.mkdirs();
}
}else {
System.out.println("Decompressing file -- " + entryName);
String fileDir = entryPath.substring(0, entryPath.lastIndexOf("/"));
File fileDirFile = new File(fileDir);
if(!fileDirFile.exists()){
fileDirFile.mkdirs();
}
BufferedOutputStream bufOut = new BufferedOutputStream(new FileOutputStream(decompressDir + "/" + entryName));
bufIn = new BufferedInputStream(zipFile.getInputStream(zipEntry));
byte[] readBuf = new byte[2048];
int readCount = bufIn.read(readBuf);
while (readCount != -1){
bufOut.write(readBuf, 0, readCount);
readCount = bufIn.read(readBuf);
}
bufOut.close();
}
}
zipFile.close();
}
示例8: deleteZipFile
import org.apache.tools.zip.ZipFile; //导入依赖的package包/类
public void deleteZipFile(File file) throws IOException {
//根据压缩包删除解压后的文件
// 用默认编码或UTF-8编码解压会乱码!windows7的原因吗
ZipFile zip = new ZipFile(file, "GBK");
ZipEntry entry;
String name;
String filename;
File directory;
//删除文件
Enumeration<ZipEntry> en = zip.getEntries();
while (en.hasMoreElements()) {
entry = en.nextElement();
if (!entry.isDirectory()) {
name = entry.getName();
filename = name;
directory = new File(realPathResolver.get(filename));
if(directory.exists()){
directory.delete();
}
}
}
//删除空文件夹
en= zip.getEntries();
while (en.hasMoreElements()) {
entry = en.nextElement();
if (entry.isDirectory()) {
name = entry.getName();
filename = name;
directory = new File(realPathResolver.get(filename));
if(!directoryHasFile(directory)){
directory.delete();
}
}
}
zip.close();
}
示例9: extract
import org.apache.tools.zip.ZipFile; //导入依赖的package包/类
/**
* @param zipFile
* @param destinationPath
* @param overwrite
* @param transformer
* @param monitor
* @return
* @throws IOException
*/
public static IStatus extract(File zipFile, File destinationPath, Conflict overwrite,
IInputStreamTransformer transformer, IProgressMonitor monitor) throws IOException
{
if (canDoNativeUnzip(overwrite, transformer))
{
return nativeUnzip(zipFile, destinationPath, overwrite, monitor);
}
ZipFile zip = new ZipFile(zipFile);
return extract(zip, zip.getEntries(), destinationPath, overwrite, transformer, monitor);
}
示例10: openEntry
import org.apache.tools.zip.ZipFile; //导入依赖的package包/类
/**
* Open input stream for specified zip entry.
*
* @param zipFile
* @param path
* @return
* @throws IOException
*/
public static InputStream openEntry(File zipFile, IPath path) throws IOException
{
ZipFile zip = new ZipFile(zipFile);
ZipEntry entry = zip.getEntry(path.makeRelative().toPortableString());
if (entry != null)
{
return zip.getInputStream(entry);
}
return null;
}
示例11: addResource
import org.apache.tools.zip.ZipFile; //导入依赖的package包/类
/**
* Add a file entry.
*/
private void addResource(final Resource r, final String name, final String prefix,
final ZipOutputStream zOut, final int mode,
final ZipFile zf, final File fromArchive)
throws IOException {
if (zf != null) {
final ZipEntry ze = zf.getEntry(r.getName());
if (ze != null) {
final boolean oldCompress = doCompress;
if (keepCompression) {
doCompress = (ze.getMethod() == ZipEntry.DEFLATED);
}
try (final BufferedInputStream is = new BufferedInputStream(zf.getInputStream(ze))) {
zipFile(is, zOut, prefix + name, ze.getTime(),
fromArchive, mode, ze.getExtraFields(true));
} finally {
doCompress = oldCompress;
}
}
} else {
try (final BufferedInputStream is = new BufferedInputStream(r.getInputStream())) {
zipFile(is, zOut, prefix + name, r.getLastModified(),
fromArchive, mode, r instanceof ZipResource
? ((ZipResource) r).getExtraFields() : null);
}
}
}
示例12: expandFile
import org.apache.tools.zip.ZipFile; //导入依赖的package包/类
/**
* This method is to be overridden by extending unarchival tasks.
*
* @param fileUtils the fileUtils
* @param srcF the source file
* @param dir the destination directory
*/
protected void expandFile(FileUtils fileUtils, File srcF, File dir) {
log("Expanding: " + srcF + " into " + dir, Project.MSG_INFO);
FileNameMapper mapper = getMapper();
if (!srcF.exists()) {
throw new BuildException("Unable to expand "
+ srcF
+ " as the file does not exist",
getLocation());
}
try (
ZipFile
zf = new ZipFile(srcF, encoding, scanForUnicodeExtraFields)){
boolean empty = true;
Enumeration<ZipEntry> e = zf.getEntries();
while (e.hasMoreElements()) {
empty = false;
ZipEntry ze = e.nextElement();
InputStream is = null;
log("extracting " + ze.getName(), Project.MSG_DEBUG);
try {
extractFile(fileUtils, srcF, dir,
is = zf.getInputStream(ze), //NOSONAR
ze.getName(), new Date(ze.getTime()),
ze.isDirectory(), mapper);
} finally {
FileUtils.close(is);
}
}
if (empty && getFailOnEmptyArchive()) {
throw new BuildException("archive '%s' is empty", srcF);
}
log("expand complete", Project.MSG_VERBOSE);
} catch (IOException ioe) {
throw new BuildException(
"Error while expanding " + srcF.getPath()
+ "\n" + ioe.toString(),
ioe);
}
}
示例13: isSigned
import org.apache.tools.zip.ZipFile; //导入依赖的package包/类
/**
* Returns <code>true</code> if the file exists and is signed with
* the signature specified, or, if <code>name</code> wasn't
* specified, if the file contains a signature.
* @param zipFile the zipfile to check
* @param name the signature to check (may be killed)
* @return true if the file is signed.
* @throws IOException on error
*/
public static boolean isSigned(File zipFile, String name)
throws IOException {
try (ZipFile jarFile = new ZipFile(zipFile)) {
if (null == name) {
Enumeration<ZipEntry> entries = jarFile.getEntries();
while (entries.hasMoreElements()) {
String eName = entries.nextElement().getName();
if (eName.startsWith(SIG_START)
&& eName.endsWith(SIG_END)) {
return true;
}
}
return false;
}
name = replaceInvalidChars(name);
boolean shortSig = jarFile.getEntry(SIG_START
+ name.toUpperCase()
+ SIG_END) != null;
boolean longSig = false;
if (name.length() > SHORT_SIG_LIMIT) {
longSig = jarFile.getEntry(
SIG_START
+ name.substring(0, SHORT_SIG_LIMIT).toUpperCase()
+ SIG_END) != null;
}
return shortSig || longSig;
}
}
示例14: unZipFile
import org.apache.tools.zip.ZipFile; //导入依赖的package包/类
private static void unZipFile(File destFile, ZipFile zipFile, ZipEntry entry)
throws IOException {
InputStream inputStream;
FileOutputStream fileOut;
if (entry.isDirectory()) // 是目录,则创建之
{
destFile.mkdirs();
} else // 是文件
{
// 如果指定文件的父目录不存在,则创建之.
File parent = destFile.getParentFile();
if (parent != null && !parent.exists()) {
parent.mkdirs();
}
inputStream = zipFile.getInputStream(entry);
fileOut = new FileOutputStream(destFile);
byte[] buf = new byte[bufSize];
int readedBytes;
while ((readedBytes = inputStream.read(buf)) > 0) {
fileOut.write(buf, 0, readedBytes);
}
fileOut.close();
inputStream.close();
}
}
示例15: unZip
import org.apache.tools.zip.ZipFile; //导入依赖的package包/类
/**
* 解压缩文件zipFile保存在directory目录下
*
* @param directory
* @param zipFile
*/
public static void unZip(String zipFile, String directory) {
try {
extractFile(new ZipFile(zipFile, ZIP_ENCODING), directory);
} catch (IOException e) {
throw new RuntimeException("Failed to process ZIP file.", e);
}
}