/** * Creates a zip archive of the currently serialized files in getCurrentDirectory(), placing the * archive in getArchiveDirectory(). * * @throws RuntimeException if clazz cannot be serialized. This exception has an informative * message and wraps the originally thrown exception as root cause. * @see #getCurrentDirectory() * @see #getArchiveDirectory() */ public void archiveCurrentDirectory() throws RuntimeException { System.out.println( "Making zip archive of files in " + getCurrentDirectory() + ", putting it in " + getArchiveDirectory() + "."); File current = new File(getCurrentDirectory()); if (!current.exists() || !current.isDirectory()) { throw new IllegalArgumentException( "There is no " + current.getAbsolutePath() + " directory. " + "\nThis is where the serialized classes should be. " + "Please run serializeCurrentDirectory() first."); } File archive = new File(getArchiveDirectory()); if (archive.exists() && !archive.isDirectory()) { throw new IllegalArgumentException( "Output directory " + archive.getAbsolutePath() + " is not a directory."); } if (!archive.exists()) { boolean success = archive.mkdirs(); } String[] filenames = current.list(); // Create a buffer for reading the files byte[] buf = new byte[1024]; try { String version = Version.currentRepositoryVersion().toString(); // Create the ZIP file String outFilename = "serializedclasses-" + version + ".zip"; File _file = new File(getArchiveDirectory(), outFilename); FileOutputStream fileOut = new FileOutputStream(_file); ZipOutputStream out = new ZipOutputStream(fileOut); // Compress the files for (String filename : filenames) { File file = new File(current, filename); FileInputStream in = new FileInputStream(file); // Add ZIP entry to output stream. ZipEntry entry = new ZipEntry(filename); entry.setSize(file.length()); entry.setTime(file.lastModified()); out.putNextEntry(entry); // Transfer bytes from the file to the ZIP file int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } // Complete the entry out.closeEntry(); in.close(); } // Complete the ZIP file out.close(); System.out.println("Finished writing zip file " + outFilename + "."); } catch (IOException e) { throw new RuntimeException( "There was an I/O error associated with " + "the process of zipping up files in " + getCurrentDirectory() + ".", e); } }