}
public void createArchive()
{
// using commons compress to allow setting filename encoding pre java7
ZipArchiveOutputStream out = null;
try
{
collectFiles(baseDir, "");
//make mimetype the first entry
int mimetype = names.indexOf("mimetype");
if (mimetype > -1)
{
String name = names.remove(mimetype);
String path = paths.remove(mimetype);
names.add(0, name);
paths.add(0, path);
}
else
{
System.err.println("No mimetype file found in expanded publication, output archive will be invalid");
}
out = new ZipArchiveOutputStream(epubFile);
out.setEncoding("UTF-8");
for (int i = 0; i < paths.size(); i++)
{
ZipArchiveEntry entry = new ZipArchiveEntry(new File(paths.get(i)), names.get(i));
if (i == 0 && mimetype > -1)
{
entry.setMethod(ZipArchiveEntry.STORED);
entry.setSize(getSize(paths.get(i)));
entry.setCrc(getCRC(paths.get(i)));
}
else
{
entry.setMethod(ZipArchiveEntry.DEFLATED);
}
out.putArchiveEntry(entry);
FileInputStream in = null;
try
{
in = new FileInputStream(paths.get(i));
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0)
{
out.write(buf, 0, len);
}
out.closeArchiveEntry();
}
finally
{
if (in != null)
{
in.close();
}
}
}
}
catch (Exception e)
{
throw new RuntimeException(e.getMessage());
}
finally
{
try
{
if (out != null)
{
out.flush();
out.finish();
out.close();
}
}
catch (IOException ignored)
{
}