Я пробовал все утро, и я не могу понять это! Я основывая свой код от this sampleЗапись папок в JAR-файл
public void run() throws IOException
{
Manifest manifest = new Manifest();
manifest.getMainAttributes().put(Attributes.Name.MANIFEST_VERSION, "1.0");
JarOutputStream target = new JarOutputStream(new FileOutputStream("output.jar"), manifest);
add(new File("inputDirectory"), target);
target.close();
}
private void add(File source, JarOutputStream target) throws IOException
{
BufferedInputStream in = null;
try
{
if (source.isDirectory())
{
String name = source.getPath().replace("\\", "/");
if (!name.isEmpty())
{
if (!name.endsWith("/"))
name += "/";
JarEntry entry = new JarEntry(name);
entry.setTime(source.lastModified());
target.putNextEntry(entry);
target.closeEntry();
}
for (File nestedFile: source.listFiles())
add(nestedFile, target);
return;
}
JarEntry entry = new JarEntry(source.getPath().replace("\\", "/"));
entry.setTime(source.lastModified());
target.putNextEntry(entry);
in = new BufferedInputStream(new FileInputStream(source));
byte[] buffer = new byte[1024];
while (true)
{
int count = in.read(buffer);
if (count == -1)
break;
target.write(buffer, 0, count);
}
target.closeEntry();
}
finally
{
if (in != null)
in.close();
}
}
Скажем, моя структура каталогов
- C:/source
-- C:/source/test.txt
--- C:/source/folder/test2.txt
----C:/source/folder/deeper/test3.txt
Я хочу, чтобы мой JAR быть структурирована следующим образом
- META-INF/manifest.MF (I've already got this part sorted)
-- test.txt
--- folder (which then contains test2 and a sub folder called deeper which in turn contains test3.txt)
Я изо всех сил, чтобы получить право рекурсии.
В приведенном выше примере кода создается папка C:\source
в моем почтовом ящике, которая явно не то, что я хочу.
Это частично работает. Я фактически разместил свои файлы в «Мои документы» (C: \ Users \ ian \ Documents \ source) и установил yourRootPath на «C: \ Users \ ian \ Documents \» и теперь получил все пользователи -> Ian -> Документы также помещаются в банку. –
Вы изменили его в обоих случаях «новый JarEntry (...)»? – masinger