Showing posts with label Java Archive. Show all posts
Showing posts with label Java Archive. Show all posts

Tuesday, November 15, 2011

How to save an archive

I'm not going to say too much, this does the trick... no grantees...



/**
 * An object that archives files and folders.
 * 
 * @author TacB0sS
 */
public class Archiver {

/**
* The file to save the archive to.
*/
private File archiveFile;

/**
* The archive manifest.
*/
private Manifest manifest;

/**
* The collection of files and folders to archive.
*/
private Vector<File> files = new Vector<File>();

/**
* Inner management file to represent the common parent file of the file collection to archive.
*/
private File parentFile;

/**
* Adds a file to the files list for archiving.
* 
* @param file The file to add.
*/
public void addFile(File file) {
if (parentFile != null)
if (!file.getParentFile().equals(parentFile))
throw new IllegalArgumentException("Multiple files with multiple parents is not allowed!");
files.add(file);
if (files.size() == 1)
parentFile = file.getParentFile();
}

/**
* @param files An array of files to add to the archive.
*/
public void addFiles(File[] files) {
for (File file : files)
addFile(file);
}

/**
* Performs the archiving process.
* 
* @return The file object with the specified file path.
* @throws IOException If an IOException has occurred while archiving
*/
public File archiveToFile()
throws IOException {
archiveFile.createNewFile();
JarOutputStream jos;
if (manifest == null)
jos = new JarOutputStream(new FileOutputStream(archiveFile));
else
jos = new JarOutputStream(new FileOutputStream(archiveFile), manifest);
writeFilesIntoArchive(files.toArray(new File[files.size()]), jos);
jos.finish();
jos.close();
return archiveFile;
}

public File getFilePath() {
return archiveFile;
}

public Manifest getManifest() {
return manifest;
}

/**
* Removes a file from the files list for archiving.
* 
* @param file The file to remove.
*/
public void removeFile(File file) {
files.remove(file);
if (files.size() == 0)
parentFile = null;
}

/**
* @param files An array of files to remove from the archive.
*/
public void removeFiles(File[] files) {
for (File file : files)
removeFile(file);
}

/**
* The file path to save the archive to.
* 
* @param archiveFile The file to save the archive to.
*/
public void setArchiveFile(File archiveFile) {
this.archiveFile = archiveFile;
}

/**
* If there is a need for manifest, set it up.
* 
* @param manifest The archive manifest.
*/
public void setManifest(Manifest manifest) {
this.manifest = manifest;
}

private void writeFilesIntoArchive(File[] files, JarOutputStream jos)
throws IOException {
for (File file : files) {
if (file.isDirectory()) {
writeFilesIntoArchive(file.listFiles(), jos);
continue;
}
writeIntoJar(file, jos);
}
}

private void writeIntoJar(File toArchive, JarOutputStream jos)
throws IOException {
byte[] buf = new byte[1024];
String relativePath = toArchive.getAbsolutePath().replace(parentFile.getAbsolutePath(), "").replace(File.separator, "/");
relativePath = relativePath.substring(1);
jos.putNextEntry(new JarEntry(relativePath));
FileInputStream fis = new FileInputStream(toArchive);
int anz;
while ((anz = fis.read(buf)) != -1)
jos.write(buf, 0, anz);
jos.closeEntry();
}
}