本文整理汇总了Java中org.apache.tools.zip.ZipFile.getInputStream方法的典型用法代码示例。如果您正苦于以下问题:Java ZipFile.getInputStream方法的具体用法?Java ZipFile.getInputStream怎么用?Java ZipFile.getInputStream使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.tools.zip.ZipFile
的用法示例。
在下文中一共展示了ZipFile.getInputStream方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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();
}
}
};
}
示例2: 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);
}
}
}
示例3: 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();
}
示例4: 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;
}
示例5: 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);
}
}
}
示例6: 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();
}
}
示例7: unzip
import org.apache.tools.zip.ZipFile; //导入方法依赖的package包/类
static private void unzip(File dir, File zipFile) throws IOException {
dir = dir.getAbsoluteFile(); // without absolutization, getParentFile below seems to fail
ZipFile zip = new ZipFile(zipFile);
@SuppressWarnings("unchecked")
Enumeration<ZipEntry> entries = zip.getEntries();
try {
while (entries.hasMoreElements()) {
ZipEntry e = entries.nextElement();
File f = new File(dir, e.getName());
if (e.isDirectory()) {
f.mkdirs();
} else {
File p = f.getParentFile();
if (p != null) {
p.mkdirs();
}
InputStream input = zip.getInputStream(e);
try {
IOUtils.copy(input, f);
} finally {
input.close();
}
try {
FilePath target = new FilePath(f);
int mode = e.getUnixMode();
if (mode!=0) // Ant returns 0 if the archive doesn't record the access mode
target.chmod(mode);
} catch (InterruptedException ex) {
LOGGER.log(Level.WARNING, "unable to set permissions", ex);
}
f.setLastModified(e.getTime());
}
}
} finally {
zip.close();
}
}
示例8: imoport
import org.apache.tools.zip.ZipFile; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public void imoport(File file, CmsSite site) throws IOException {
String resRoot = site.getResPath();
String tplRoot = site.getTplPath();
// 用默认编码或UTF-8编码解压会乱码!windows7的原因吗?
ZipFile zip = new ZipFile(file, "GBK");
ZipEntry entry;
String name;
String filename;
File outFile;
File pfile;
byte[] buf = new byte[1024];
int len;
InputStream is = null;
OutputStream os = null;
String solution = null;
Enumeration<ZipEntry> en = zip.getEntries();
while (en.hasMoreElements()) {
name = en.nextElement().getName();
if (!name.startsWith(RES_EXP)) {
solution = name.substring(0, name.indexOf("/"));
break;
}
}
if (solution == null) {
return;
}
en = zip.getEntries();
while (en.hasMoreElements()) {
entry = en.nextElement();
if (!entry.isDirectory()) {
name = entry.getName();
log.debug("unzip file:{}", name);
// 模板还是资源
if (name.startsWith(RES_EXP)) {
filename = resRoot + "/" + solution
+ name.substring(RES_EXP.length());
} else {
filename = tplRoot + SPT + name;
}
log.debug("解压地址:{}", filename);
outFile = new File(realPathResolver.get(filename));
pfile = outFile.getParentFile();
if (!pfile.exists()) {
pfile.mkdirs();
}
try {
is = zip.getInputStream(entry);
os = new FileOutputStream(outFile);
while ((len = is.read(buf)) != -1) {
os.write(buf, 0, len);
}
} finally {
if (is != null) {
is.close();
is = null;
}
if (os != null) {
os.close();
os = null;
}
}
}
}
}
示例9: unZipFile
import org.apache.tools.zip.ZipFile; //导入方法依赖的package包/类
public void unZipFile(File file) throws IOException {
// 用默认编码或UTF-8编码解压会乱码!windows7的原因吗?
//解压之前要坚持是否冲突
ZipFile zip = new ZipFile(file, "GBK");
ZipEntry entry;
String name;
String filename;
File outFile;
File pfile;
byte[] buf = new byte[1024];
int len;
InputStream is = null;
OutputStream os = null;
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()){
break;
}
pfile = outFile.getParentFile();
if (!pfile.exists()) {
// pfile.mkdirs();
createFolder(outFile);
}
try {
is = zip.getInputStream(entry);
os = new FileOutputStream(outFile);
while ((len = is.read(buf)) != -1) {
os.write(buf, 0, len);
}
} finally {
if (is != null) {
is.close();
is = null;
}
if (os != null) {
os.close();
os = null;
}
}
}
}
zip.close();
}
示例10: unzip
import org.apache.tools.zip.ZipFile; //导入方法依赖的package包/类
/**
* 使用 org.apache.tools.zip.ZipFile 解压文件,它与 java 类库中的 java.util.zip.ZipFile
* 使用方式是一新的,只不过多了设置编码方式的 接口。
* 注,apache 没有提供 ZipInputStream 类,所以只能使用它提供的ZipFile 来读取压缩文件。
*
* @param archive
* 压缩包路径
* @param decompressDir
* 解压路径
* @throws IOException
* IOException
* @throws FileNotFoundException
* FileNotFoundException
* @throws ZipException
* ZipException
*/
public synchronized static void unzip(String archive, String decompressDir)
throws IOException, FileNotFoundException, ZipException {
BufferedInputStream bi;
ZipFile zf = new ZipFile(archive, encoding);// 支持中文
Enumeration<ZipEntry> e = zf.getEntries();
while (e.hasMoreElements()) {
ZipEntry ze = e.nextElement();
String entryName = ze.getName();
String path = decompressDir + "/" + entryName;
if (ze.isDirectory()) {
logger.debug("正在创建解压目录 - " + entryName);
File decompressDirFile = new File(path);
if (!decompressDirFile.exists()) {
decompressDirFile.mkdirs();
}
} else {
logger.debug("正在创建解压文件 - " + entryName);
String fileDir = path.substring(0, path.lastIndexOf("/"));
File fileDirFile = new File(fileDir);
if (!fileDirFile.exists()) {
fileDirFile.mkdirs();
}
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream(decompressDir + "/" + entryName));
bi = new BufferedInputStream(zf.getInputStream(ze));
byte[] readContent = new byte[1024];
int readCount = bi.read(readContent);
while (readCount != -1) {
bos.write(readContent, 0, readCount);
readCount = bi.read(readContent);
}
bos.close();
bi.close();
}
}
zf.close();
}