62,614
社区成员
发帖
与我相关
我的任务
分享
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);
}
}
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)
/**
* @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();
}
}
/**
* @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();
}
}
/**
* @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();
}
}