{
File archiveLocation = new File(dir.getParentFile() + File.separator + dir.getName() + ".tar.gz");
boolean unixArchiveFormat = true;
TarArchive archive = null;
OutputStream outStream = System.out;
if (outStream != null)
{
try
{
outStream = new GZIPOutputStream(new FileOutputStream(archiveLocation));
}
catch (IOException ex)
{
outStream = null;
ex.printStackTrace(System.err);
}
archive = new TarArchive(outStream, TarBuffer.DEFAULT_BLKSIZE);
}
// write
File[] files = dir.listFiles();
for (File file : files)
{
if (!file.isDirectory())
{
// make regular entry
TarEntry entry = new TarEntry(file);
if (unixArchiveFormat)
{
entry.setUnixTarFormat();
}
else
{
entry.setUSTarFormat();
}
// FIXME: index -1 wanneer verkeerde file sep!
entry.setName(entry.getName().substring(entry.getName().lastIndexOf("/") + 1, entry.getName().length()));
// add to tar. just a file, no need for recursion ('false')
archive.writeEntry(entry, false);
}
else
{
// entry is a directory, so tar the content :)
TarEntry entry = new TarEntry(TarGz.tarDir(file));
if (unixArchiveFormat)
{
entry.setUnixTarFormat();
}
else
{
entry.setUSTarFormat();
}
// FIXME: index -1 wanneer verkeerde file sep!
entry.setName(entry.getName().substring(entry.getName().lastIndexOf("/") + 1, entry.getName().length()));
// write entry (now a tar) to tar
archive.writeEntry(entry, false);
}
}
// close
if (archive != null) // CLOSE ARCHIVE
{
try
{
archive.closeArchive();
}
catch (IOException ex)
{
ex.printStackTrace(System.err);
}