我需要在不使用eclipse的情况下导出(java)eclipse项目。我尝试复制所有相关文件并使用ZipOutputStream打包它们。到目前为止这么好,我可以在Windows中加载这个zip文件。但 MAC用户有问题,文件结构未自动检测,并在旁边创建名称为“vorlage \ src \ de \ tuberlin ...”的文件src目录。 (example picture)
有没有办法用java程序正确导出eclipse项目?
原因是我为一个讲座创建了编程练习,并用特别注释标记了解决方案。在复制文件期间,这些注释和解决方案都被遗漏了。
一种解决方案是在Windos中导入zip文件并在eclipse的帮助下再次导出。但是每次练习都需要花费大量时间。
虽然我看不出这两个zip文件有什么区别,但似乎有些东西。
这是我复制文件的代码:
public static void copyDir(File source, File target, ArrayList<String> exclude)
throws FileNotFoundException, IOException {
File[] files = source.listFiles();
File newFile = null;
target.mkdirs();
if (files != null) {
for (int i = 0; i < files.length; i++) {
newFile = new File(target.getAbsolutePath() + System.getProperty("file.separator") + files[i].getName());
if (files[i].isDirectory()) {
copyDir(files[i], newFile, exclude);
}
else if ( !exclude.contains(files[i].getName()) ) {
copyFile(files[i], newFile);
}
}
}
}
public static void copyFile(File file, File target) throws FileNotFoundException, IOException {
if (file.getName().endsWith(".java")) {
copyJavaFile(file, target);
return;
}
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(target, true));
int bytes = 0;
while ((bytes = in.read()) != -1) {
out.write(bytes);
}
in.close();
out.close();
}
private static void copyJavaFile(File file, File target) throws IOException {
BufferedReader in = new BufferedReader(new FileReader(file));
PrintWriter out = new PrintWriter(target);
boolean stopped = false;
String line = null;
while ( (line = in.readLine() ) != null) {
stopped |= line.contains("IF EXCLUDE");
if (!stopped && !line.contains("ENDIF"))
out.println(line);
stopped &= !line.contains("ELSE");
}
in.close();
out.close();
}
我复制以下(子)目录/文件:
我使用以下命令创建zip文件:
public Zip(final File projDir, final File outFile) {
ZipOutputStream output = null;
try {
output = new ZipOutputStream(
new BufferedOutputStream(
new FileOutputStream(outFile)));
for (File file : projDir.listFiles()) {
writeFiles(file, output, "");
}
output.finish();
} catch (IOException ex) {
System.err.println("IO Error: " + ex.getMessage());
} finally {
try {output.close();} catch (IOException ex) {}
}
}
private void writeFiles(File f, ZipOutputStream output, String dir) throws IOException {
if (f.isDirectory()) {
// recursively write files in directory
for (File file : f.listFiles()) {
writeFiles(file, output, dir + f.getName() + File.separator);
}
} else {
// write this file to archive
FileInputStream fis = new FileInputStream(f);
ZipEntry entry = new ZipEntry(dir + f.getName());
entry.setTime(f.lastModified());
output.putNextEntry(entry);
copy(fis, output);
output.closeEntry();
fis.close();
}
}
private void copy(InputStream is, OutputStream os) throws IOException {
byte[] buffer = new byte[1024];
int bytes;
while ((bytes = is.read(buffer)) > 0) {
os.write(buffer, 0, bytes);
}
}
答案 0 :(得分:0)
这是路径分隔符的问题。使用&#34; /&#34;而不是&#34; File.separator&#34;解决了这个问题。 在上面的示例中,writeFiles(...)方法中的for循环必须是:
for (File file : f.listFiles()) {
writeFiles(file, output, dir + f.getName() + "/");
}
另见this answer。