当前位置: 技术问答>java相关
求教高手:从JAR文件中读出文件的问题 ----望速回!!!
来源: 互联网 发布时间:2015-07-30
本文导语: package untitled5; import java.util.*; import java.util.jar.*; import java.util.zip.*; import java.io.*; /** * Reads GZIP, Zip, and Jar files and outputs their content to the console. */ class Zip2 { public static void main(String[] args) { ...
package untitled5;
import java.util.*;
import java.util.jar.*;
import java.util.zip.*;
import java.io.*;
/** * Reads GZIP, Zip, and Jar files and outputs their content to the console. */
class Zip2 { public static void main(String[] args) {
String fileName = "untitled5.jar";
// String fileName = args[0];
System.out.println("contents of " + fileName + "...");
if (fileName.endsWith(".jar")) { readJarFile(fileName); } }
/** * Reads a Jar file, displaying the attributes in its manifest and dumping
* the contents of each file contained to the console. */
private static void readJarFile(String fileName) { JarFile jarFile = null;
try { // JarFile extends ZipFile and adds manifest information
jarFile = new JarFile(fileName);
File outFile = new File("tempFile.java");
FileOutputStream fOut = new FileOutputStream(outFile);
if (jarFile.getManifest() != null) { System.out.println("Manifest Main Attributes:");
Iterator iter = jarFile.getManifest().getMainAttributes().keySet().iterator();
while (iter.hasNext()) {
Attributes.Name attribute = (Attributes.Name) iter.next();
System.out.println(attribute + " : " + jarFile.getManifest().getMainAttributes().getValue(attribute)); }
System.out.println(); }
// use the Enumeration to dump the contents of each file to the console
System.out.println("Jar file entries:");
for (Enumeration e = jarFile.entries(); e.hasMoreElements();) {
JarEntry jarEntry = (JarEntry) e.nextElement();
if (!jarEntry.isDirectory()) {
if (jarEntry.getName().toString()!="Applet1.html"){
System.out.println(jarEntry.getName() + " contains:");
BufferedReader jarReader = new BufferedReader(
new InputStreamReader(jarFile.getInputStream(jarEntry)));
while (jarReader.ready()) {
// System.out.println(jarReader.readLine());
int c;
while ((c = jarReader.read()) > -1)
{
fOut.write(c);
}
}
jarReader.close();
}
}}
}
catch (IOException ioe) {
System.out.println("An IOException occurred: " + ioe.getMessage()); }
finally { if (jarFile != null) {
try { jarFile.close(); }
catch (IOException ioe) {} } } } }
问题: 为什么判断文件名"Applet1.html"时失败? 本程序还有没有其他问题?
import java.util.*;
import java.util.jar.*;
import java.util.zip.*;
import java.io.*;
/** * Reads GZIP, Zip, and Jar files and outputs their content to the console. */
class Zip2 { public static void main(String[] args) {
String fileName = "untitled5.jar";
// String fileName = args[0];
System.out.println("contents of " + fileName + "...");
if (fileName.endsWith(".jar")) { readJarFile(fileName); } }
/** * Reads a Jar file, displaying the attributes in its manifest and dumping
* the contents of each file contained to the console. */
private static void readJarFile(String fileName) { JarFile jarFile = null;
try { // JarFile extends ZipFile and adds manifest information
jarFile = new JarFile(fileName);
File outFile = new File("tempFile.java");
FileOutputStream fOut = new FileOutputStream(outFile);
if (jarFile.getManifest() != null) { System.out.println("Manifest Main Attributes:");
Iterator iter = jarFile.getManifest().getMainAttributes().keySet().iterator();
while (iter.hasNext()) {
Attributes.Name attribute = (Attributes.Name) iter.next();
System.out.println(attribute + " : " + jarFile.getManifest().getMainAttributes().getValue(attribute)); }
System.out.println(); }
// use the Enumeration to dump the contents of each file to the console
System.out.println("Jar file entries:");
for (Enumeration e = jarFile.entries(); e.hasMoreElements();) {
JarEntry jarEntry = (JarEntry) e.nextElement();
if (!jarEntry.isDirectory()) {
if (jarEntry.getName().toString()!="Applet1.html"){
System.out.println(jarEntry.getName() + " contains:");
BufferedReader jarReader = new BufferedReader(
new InputStreamReader(jarFile.getInputStream(jarEntry)));
while (jarReader.ready()) {
// System.out.println(jarReader.readLine());
int c;
while ((c = jarReader.read()) > -1)
{
fOut.write(c);
}
}
jarReader.close();
}
}}
}
catch (IOException ioe) {
System.out.println("An IOException occurred: " + ioe.getMessage()); }
finally { if (jarFile != null) {
try { jarFile.close(); }
catch (IOException ioe) {} } } } }
问题: 为什么判断文件名"Applet1.html"时失败? 本程序还有没有其他问题?
|
!"Applet1.html".equals(jarEntry.getName().toString())
|
java里面判断字符串是否相等用equals函数。
|
if (!jarEntry.getName().toString().equals("Applet1.html")){}