The Zip format is also used in the JAR (Java ARchive) file format, which is a way to collect a group of files into a single compressed file, just like Zip. However, like everything else in Java, JAR files are cross-platform, so you don’t need to worry about
JAR files are particularly helpful when you deal with the Internet. Before JAR files, your Web browser would have to make repeated requests of a Web server in order to download all of the files that make up an applet. In addition, each of these files was uncompressed. By combining all of the files for a particular applet into a single JAR file, only one server request is necessary and the transfer is faster because of compression. And each entry in a JAR file can be digitally signed for security (see Chapter 14 for an example of signing).
A JAR file consists of a single file containing a collection of zipped files along with a “manifest” that describes them. (You can create your own manifest file; otherwise, the jar program will do it for you.) You can find out more about JAR manifests in the
The jar utility that comes with Sun’s JDK automatically compresses the files of your choice. You invoke it on the command line:
jar [options] destination [manifest] inputfile(s)
The options are simply a collection of letters (no hyphen or any other indicator is necessary). Unix/Linux users will note the similarity to the tar options. These are:
c |
Creates a new or empty archive. |
t |
Lists the table of contents. |
x |
Extracts all files. |
x file |
Extracts the named file. |
f |
Says: “I’m going to give you the name of the file.” If you don’t use this, jar assumes that its input will come from standard input, or, if it is creating a file, its output will go to standard output. |
m |
Says that the first argument will be the name of the user-created manifest file. |
v |
Generates verbose output describing what jar is doing. |
0 |
Only store the files; doesn’t compress the files (use to create a JAR file that you can put in your classpath). |
M |
Don’t automatically create a manifest file. |
If a subdirectory is included in the files to be put into the JAR file, that subdirectory is automatically added, including all of its subdirectories, etc. Path information is also preserved.
Here are some typical ways to invoke jar:
jar cf myJarFile.jar *.class
This creates a JAR file called myJarFile.jar that contains all of the class files in the current directory, along with an automatically generated manifest file.
jar cmf myJarFile.jar myManifestFile.mf *.class
Like the previous example, but adding a user-created manifest file called myManifestFile.mf.
jar tf myJarFile.jar
Produces a table of contents of the files in myJarFile.jar.
jar tvf myJarFile.jar
Adds the “verbose” flag to give more detailed information about the files in myJarFile.jar.
jar cvf myApp.jar audio classes image
Assuming audio, classes, and image are subdirectories, this combines all of the subdirectories into the file myApp.jar. The “verbose” flag is also included to give extra feedback while the jar program is working.
If you create a JAR file using the 0 (zero) option, that file can be placed in your CLASSPATH:
CLASSPATH="lib1.jar;lib2.jar;"
Then Java can search lib1.jar and lib2.jar for class files.
The jar tool isn’t as useful as a zip utility. For example, you can’t add or update files to an existing JAR file; you can create JAR files only from scratch. Also, you can’t move files into a JAR file, erasing them as they are moved. However, a JAR file created on one platform will be transparently readable by the jar tool on any other platform (a problem that sometimes plagues zip utilities).
As you will see in Chapter 14, JAR files are also used to
출처 : http://www.linuxtopia.org/online_books/programming_books/thinking_in_java/TIJ314_034.htm
'Language > JAVA' 카테고리의 다른 글
FILE을 CLOB에 INSERT하고, 반대로 컬럼을 읽어 FILE로 WRITE하는 JDBC PROGRAM 예제 (JDBC 8.1 이상 (0) | 2011.01.07 |
---|---|
Heap Dump 분석을 통한 Perm Area Memory Leak 원인 진단 (0) | 2010.12.14 |
Multifile storage with Zip (ZIP으로 여러 파일 저장하기) (0) | 2010.11.17 |
자바 간단한 GZIP 압축 예제 (0) | 2010.11.17 |
자바 파일 압축 (0) | 2010.11.17 |