public static void main(String[] args) throws IOException {
File f = new File("/Users/user/Desktop/META-INF");
if(f.exists()) {
System.out.println("Test~");
InputStream is = new FileInputStream(f);
}
}
/Users/user/Desktop/META-INF 这个目录是存在的, META-INF 是一个目录!
报错:
Test~
Exception in thread "main" java.io.FileNotFoundException: /Users/user/Desktop/META-INF (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at org.bruce.vertices.asist.utils.ZipUtil.main(ZipUtil.java:247)
File f = new File("/Users/user/Desktop/META-INF");
if(f.exists()) {
System.out.println("Test~");
InputStream is = new FileInputStream(f);
}
}
/Users/user/Desktop/META-INF 这个目录是存在的, META-INF 是一个目录!
报错:
Test~
Exception in thread "main" java.io.FileNotFoundException: /Users/user/Desktop/META-INF (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at org.bruce.vertices.asist.utils.ZipUtil.main(ZipUtil.java:247)
exists()是判断是否存在文件或者目录
exists()是判断是否存在文件或者目录
我现在的需求是将一个由 jar 包(实际上就是 zip 压缩包)解压出来的文件夹,再以 java 的方式将该文件夹压缩成 jar 包。
问题便是在这个过程中出现的,
我粘贴一点儿代码吧,功用如前所述,
/**
* @param dir
*/
public static void zipFile(File dir) {
String pathDir = dir.getAbsolutePath();
List<String> pathList = new LinkedList<String>();
Functions.listFilesAndDirs(pathDir, pathList);
String pathNewJar = pathDir + ".jar";
File fileNewJar = new File(pathNewJar);
JarOutputStream jos = null;
try {
FileOutputStream fos = new FileOutputStream(fileNewJar);
jos = new JarOutputStream(fos);
for(String pathItem : pathList) {
// jar 中的每一个文件夹 每一个文件 都是一个 jarEntry
String entryPath = pathItem.replace(pathDir + File.separator, "");
JarEntry newEntry = new JarEntry(entryPath); // 表示将该 entry 写入 jar 文件中 也就是创建该文件夹和文件~
jos.putNextEntry(newEntry);
/**
* Added by Bruce Yang on 2012.09.11.15.37~
* 对于文件夹,是不能获取他的 FileInputStream 的,蛋疼~
*/
File fileItem = new File(pathItem);
if(fileItem.isFile()) {
InputStream is = new FileInputStream(fileItem);
byte[] bytes = Functions.inputStream2byteArray(is);
is.close();
// 然后就是往entry中的jj.txt文件中写入内容
jos.write(bytes);
}
}
// 最后不能忘记关闭流
jos.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
这个是我之前的代码,当为目录的时候,我判断了一下不给获取流
但是我发现打出来的 jar 包,执行以下是损坏的
我现在的需求是将一个由 jar 包(实际上就是 zip 压缩包)解压出来的文件夹,再以 java 的方式将该文件夹压缩成 jar 包。
问题便是在这个过程中出现的,
我粘贴一点儿代码吧,功用如前所述,
/**
* @param dir
*/
public static void zipFile(File dir) {
String pathDir = dir.getAbsolutePath();
List<String> pathList = new LinkedList<String>();
Functions.listFilesAndDirs(pathDir, pathList);
String pathNewJar = pathDir + ".jar";
File fileNewJar = new File(pathNewJar);
JarOutputStream jos = null;
try {
FileOutputStream fos = new FileOutputStream(fileNewJar);
jos = new JarOutputStream(fos);
for(String pathItem : pathList) {
// jar 中的每一个文件夹 每一个文件 都是一个 jarEntry
String entryPath = pathItem.replace(pathDir + File.separator, "");
JarEntry newEntry = new JarEntry(entryPath); // 表示将该 entry 写入 jar 文件中 也就是创建该文件夹和文件~
jos.putNextEntry(newEntry);
/**
* Added by Bruce Yang on 2012.09.11.15.37~
* 对于文件夹,是不能获取他的 FileInputStream 的,蛋疼~
*/
File fileItem = new File(pathItem);
if(fileItem.isFile()) {
InputStream is = new FileInputStream(fileItem);
byte[] bytes = Functions.inputStream2byteArray(is);
is.close();
// 然后就是往entry中的jj.txt文件中写入内容
jos.write(bytes);
}
}
// 最后不能忘记关闭流
jos.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
这个是我之前的代码,当为目录的时候,我判断了一下不给获取流
但是我发现打出来的 jar 包,执行以下是损坏的
我现在的需求是将一个由 jar 包(实际上就是 zip 压缩包)解压出来的文件夹,再以 java 的方式将该文件夹压缩成 jar 包。
问题便是在这个过程中出现的,
我粘贴一点儿代码吧,功用如前所述,
/**
* @param dir
*/
public static void zipFile(File dir) {
String pathDir = dir.getAbsolutePath();
List<String> pathList = new LinkedList<String>();
Functions.listFilesAndDirs(pathDir, pathList);
String pathNewJar = pathDir + ".jar";
File fileNewJar = new File(pathNewJar);
JarOutputStream jos = null;
try {
FileOutputStream fos = new FileOutputStream(fileNewJar);
jos = new JarOutputStream(fos);
for(String pathItem : pathList) {
// jar 中的每一个文件夹 每一个文件 都是一个 jarEntry
String entryPath = pathItem.replace(pathDir + File.separator, "");
JarEntry newEntry = new JarEntry(entryPath); // 表示将该 entry 写入 jar 文件中 也就是创建该文件夹和文件~
jos.putNextEntry(newEntry);
/**
* Added by Bruce Yang on 2012.09.11.15.37~
* 对于文件夹,是不能获取他的 FileInputStream 的,蛋疼~
*/
File fileItem = new File(pathItem);
if(fileItem.isFile()) {
InputStream is = new FileInputStream(fileItem);
byte[] bytes = Functions.inputStream2byteArray(is);
is.close();
// 然后就是往entry中的jj.txt文件中写入内容
jos.write(bytes);
}
}
// 最后不能忘记关闭流
jos.close();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
这个是我之前的代码,当为目录的时候,我判断了一下不给获取流
但是我发现打出来的 jar 包,执行以下是损坏的