public void write (File dest, File destroot)
        throws PackageBuilderException, IOException
    {
        if (dest == null) throw new IllegalArgumentException("The destination cannot be null.");
        if (destroot == null) throw new IllegalArgumentException("The destroot cannot be null.");

        PackageTarFile dataTar = null;
        try {
            // create the temporary data.tar.gz file in the destination location, which we assume
            // has enough available space to construct both the data.tar.gz and the package.
            dataTar = new PackageTarFile(dest.getParentFile(), _info.getPermissionsMap());
            dataTar.addDirectory(destroot);
            dataTar.close();

            // Overwrite any file at the destination location.
            if (dest.exists()) {
                if (!dest.delete()) {
                    throw new PackageBuilderException(
                        "Unable to overwrite existing package destination. path=[" + dest.getAbsolutePath() + "].");
                }
            }

            // create the ar(1) archive which is the package itself
            try {
                final Archive archive;
                File postinst = _info.getPalmScript(MaintainerScript.Type.POSTINST);
                File prerm = _info.getPalmScript(MaintainerScript.Type.PRERM);
                if(postinst!=null || prerm!=null) {
                    archive = new Archive(dest, postinst, prerm);
                } else {
                    archive = new Archive(dest);
                }
                
                // add the standard header to the package
                final ArchiveEntry entry = new ArchiveStringEntry(AR_MAGIC_CONTENTS, AR_MAGIC_FILE);
                archive.appendEntry(entry);

                // add the control.tar.gz file to the package
                final ControlFile control = new ControlFile(_info, dataTar);
                archive.appendEntry(control);

                // add the data.tar.gz file to the package
                archive.appendEntry(dataTar);

                archive.finalize();
            } catch (final ArchiveException ae) {
                throw new PackageBuilderException(ae);

            } catch (final ScriptDataTooLargeException sdtle) {
                throw new PackageBuilderException(sdtle);
            } catch (final Throwable e) {}

        } finally {
            dataTar.delete();
        }
    }