- 浏览: 73538 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
george.gu:
lqjacklee 写道怎么解决。。 First: Conf ...
Bad version number in .class file -
lqjacklee:
怎么解决。。
Bad version number in .class file -
flyqantas:
would you pleade left more mate ...
UML Extension
JDK provide a set of utils to compress and decompress data by zip format. You can find technical guide from http://java.sun.com/developer/technicalArticles/Programming/compression/.
Here is my sumarrize during study how to use java zip utils. You can find those APIs from java.util.zip.*.
ZipEntry:
Represents a ZIP file entry inside ZIP content. It could be a file or a directory.
public boolean isDirectory() |
Returns true if this is a directory entry. |
public long getCompressedSize() |
Returns the compressed size of the entry, -1 if not known |
public int getMethod() |
Returns the compression method of the entry, -1 if not specified |
public String getName() |
Returns the name of the entry |
public long getSize() |
Returns the uncompressed zip of the entry, -1 if unknown |
public long getTime() |
Returns the modification time of the entry, -1 if not specified |
public void setComment(String c) |
Sets the optional comment string for the entry |
public void setMethod(int method) |
Sets the compression method for the entry |
public void setSize(long size) |
Sets the uncompressed size of the entry |
public void setTime(long time) |
Sets the modification time of the entry |
ZipInputStream: Decompressing and Extract data from a ZIP file
ZipInputStream is used to decompress and extract data from Zip file.
A ZipInputStream can be created just like any other input stream. For example, the following segment of code can be used to create an input stream for reading data from a ZIP file format:
FileInputStream fis = new FileInputStream("figs.zip");
ZipInputStream zin = new ZipInputStream(new BufferedInputStream(fis));
Once a ZIP input stream is opened, you can read the zip entries using the getNextEntry method which returns a ZipEntry object. If the end-of-file is reached, getNextEntry returns null:
ZipEntry entry;
while((entry = zin.getNextEntry()) != null) {
// extract data
// open output streams
}
ZipOutputStream: Compressing and Archiving Data in a ZIP File
The ZipOutputStream can be used to compress data to a ZIP file. The ZipOutputStream writes data to an output stream in a ZIP format.
If you output a entry like "path1/path2/filename.xml", ZipOutputStream will automatically create two additional directory entry "path1/" and "path1/path2/".
Here is a utility method to compress data into dedicated entry:
/** * Add a Zip entry with a specified name to an output stream * and write the bytes using the compression method * @param entryName name of Zip entry * @param zipos a Zip output stream * @param bytes byte array corresponding to new Zip entry * @throws IOException if output stream cannot be written */ public static void doZip(String entryName, ZipOutputStream zipos, byte[] bytes) throws IOException { ZipEntry ze = new ZipEntry(entryName); int size = bytes.length; ze.setSize(size);
zipos.putNextEntry(ze); zipos.write(bytes, 0, size); zipos.closeEntry(); } |
ZipFile:
ZipFile is used to read entries from a ZIP file. You can use ZipFile to decompress Zip file. It is like a wrapper to ZipInputStream.
import java.io.*; import java.util.*; import java.util.zip.*; public class UnZip2 { static final int BUFFER = 2048; public static void main (String args[]) { try { BufferedOutputStream dest = null; BufferedInputStream is = null; ZipEntry entry; ZipFile zipfile = new ZipFile("myfile.zip"); Enumeration e = zipfile.entries(); while(e.hasMoreElements()) { entry = (ZipEntry) e.nextElement(); System.out.println("Extracting: " +entry); is = new BufferedInputStream(zipfile.getInputStream(entry)); int count; byte data[] = new byte[BUFFER]; FileOutputStream fos = new FileOutputStream(entry.getName()); dest = new BufferedOutputStream(fos, BUFFER); while ((count = is.read(data, 0, BUFFER))!= -1) { dest.write(data, 0, count); } dest.flush(); dest.close(); is.close(); } } catch(Exception e) { e.printStackTrace(); } } } |
Please Note:
- The ZipInputStream class reads ZIP files sequentially. However class ZipFile reads the contents of a ZIP file using a random access file internally so that the entries of the ZIP file do not have to be read sequentially.
- Zip entries are not cached when the file is read using a combination of ZipInputStream and FileInputStream. However, if the file is opened using ZipFile(fileName) then it is cached internally, so if ZipFile(fileName) is called again the file is opened only once. The cached value is used on the second open.
Compressing Object
You can also use Zip utils to compress and decompress java object. Let's talk about that later.
发表评论
-
javax.naming.CommunicationException: remote side declared peer gone on this JVM.
2012-07-11 09:44 2379javax.naming.ServiceUnavailable ... -
Generate special format numbers
2012-04-27 00:06 938DecimalFormat df = new DecimalF ... -
Singleton Service in Weblogic Cluster
2012-03-21 00:12 712http://blog.csdn.net/mobicents/ ... -
Scheduled ThreadPool Executor suppressed or stopped after error happen
2012-03-20 16:54 1043ScheduledThreadPoolExecutor ... -
Bad version number in .class file
2012-01-27 00:35 895Bad version number in .class fi ... -
User Data Header in SMPP SUBMIT_SM
2012-01-25 22:30 2333SMPP optional Parameters for ... -
jQuery study
2011-12-28 00:44 0to be study -
Java is Pass-by-Value or Pass-by-Reference?
2011-12-19 19:18 687What's saved in Object Referenc ... -
java.util.Properties: a subclass of java.util.Hashtable
2011-12-13 06:57 775I met a problem this afternoon ... -
Jmock usage
2011-12-02 05:37 0Discuss how Jmock working. -
Oracle Index Usage
2011-12-15 05:26 625Like a hash mapping for record' ... -
AOP(2):AOP与动态代理JDK Proxy and Cglib Proxy
2011-05-12 16:20 1027使用动态代理(JDK Proxy 或者Cglib Proxy) ... -
AOP(1):应用中的几个小故事
2011-05-09 21:49 975I had heared about AOP almost 7 ... -
异步系统设计:push vs pull
2011-05-02 23:59 1139今天讨论问题时,有个同事说系统A是主动去系统B里“拿”消息,我 ... -
Velocity Usage
2011-04-28 22:52 1006You can find velocity mannua ... -
Java Regular Expression (Java正则表达式)
2011-04-23 06:58 933In current Project, we need to ... -
XML Parser:DOM + XPath
2011-04-23 06:30 1202There are many kinds of XML Par ... -
File upload and download in Java Web Application.
2011-04-21 21:08 1715最近在项目中遇到一个下载文件的老问题。之所以说是老问题,因为在 ... -
Beanshell: how and where to use beanshell
2011-04-21 00:33 2096How to use beanshell beansh ... -
OXM: JAXB2.0 in JDK1.6
2011-04-20 22:53 12411.1.1 JAXB 2.0: ObjectàXML ...
相关推荐
IBM FileNet P8 v4.5 Content Engine Development Java API 打包成chm格式,看起来方便
《Bluetooth Application Programming with the Java APIs Essentials Edition》是一本专门介绍如何使用Java技术进行蓝牙应用开发的专业书籍。该书由Morgan Kaufmann出版社出版,首次发布于2008年2月。本书的主要...
在提供的 `Gearman_java_Demo.zip` 文件中,你应该能找到一个简单的 Gearman Java 示例。这个示例包括了客户端和工作者两部分,客户端提交一个字符串反转的任务,工作者接收到任务后进行反转并返回结果。 运行这个 ...
In this book, you will learn about the nitty-gritty of automated trading and have a closer look at Java, the Spring Framework, event-driven programming, and other open source APIs, notably Google's ...
"xml-apis.zip" 是一个包含Java XML API的压缩包,它提供了处理XML所需的基本接口和类。 "xml-apis.zip" 中的"xml-apis.jar" 文件是XML API的核心库,包含了多个与XML处理相关的Java包,如javax.xml、org.w3c.dom和...
Java™ Media APIs是Java开发平台中的一个重要组成部分,它为开发者提供了处理多媒体数据的强大工具。这个chm文件,"Sams.Java.Media.APIs.eBook-LiB.chm",很可能是关于Java媒体APIs的详细指南或参考文档,包含了...
Java APIs, Extensions and Libraries_With JavaFX, JDBC, jmod, jlink, Networking, and the Process API-Apress(2018).pdf I am pleased to present the second edition of the Java APIs, Extensions, and ...
This release modernizes support for many industry standards and continues simplification of enterprise ready APIs. Enhancements include: Java Servlet 4.0 API with HTTP/2 support Enhanced JSON ...
《蓝牙应用编程与Java API》一书是针对蓝牙技术与Java编程结合的深入研究与实践指南,由CBalaKumar、PaulJ.Kline和TimothyJ.Thompson共同编写,属于TheMorganKaufmannSeriesinNetworking系列,该系列由MIT的大卫·...
《使用Java APIs进行蓝牙应用程序编程》是一本专为IT专业人士准备的指南,旨在深入探讨如何利用Java技术开发蓝牙应用。本书由Morgan Kaufmann出版社在2008年出版,聚焦于蓝牙通信协议栈与Java API的结合,帮助开发者...
Api-apis.zip,让任何感兴趣的人都可以随时获得数据。这是自2012年以来让数据变得漂亮!,一个api可以被认为是多个软件设备之间通信的指导手册。例如,api可用于web应用程序之间的数据库通信。通过提取实现并将数据...
Connect to databases and access data from Java programs using the JDBC API Work with JavaFX, RMI (Remote Method Invocation), and JNI (Java Native Interface) Use the new scripting features of Java
Java APIs, Extensions and Libraries With JavaFX, JDBC, jmod, jlink, Networking, and the Process API(2nd) 英文epub 第2版 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国...
Java APIs, Extensions and Libraries With JavaFX, JDBC, jmod, jlink, Networking, and the Process API(2nd) 英文无水印原版pdf 第2版 pdf所有页面使用FoxitReader、PDF-XChangeViewer、SumatraPDF和Firefox...
在 iOS9 之前,你只能在 spotlight 中输入...在 iOS9 中苹果提供了一套 Search APIs。允许开发者选择应用的内容,索引起来供 spotlight 进行搜索,同时可以设置在 spotlight 中的展示效果,以及点击之后如何响应。
After completing Pro JPA 2 in Java EE 8, you will have a full understanding of JPA and be able to successfully code applications using its annotations and APIs. The book also serves as an excellent ...
`xml-apis-1.4.01.jar` 是一个包含XML API实现的Java库,它提供了处理XML文档所需的基本接口和类。在Java开发中,如果遇到“xml-apis-1.4.01.jar does not exist”的错误,通常意味着项目缺少了对XML解析的支持。 ...
Java(TM) EE 8 Specification APIs,即Java(TM) EE 8规范API。本CHM文档是根据由javadoc(1.8.0_144)于2017年9月生成的HTML文档制作而成的,原版英文文档。