private void addFilesToArchive(ZipArchiver archiver, File sourceDir, File packageXml) throws Exception
    {
        Collection<String> documentNames;
        getLog().info(String.format("Using the existing package.xml descriptor at [%s]", packageXml.getPath()));
        try {
            documentNames = getDocumentNamesFromXML(packageXml);
        } catch (Exception e) {
            getLog().error(String.format("The existing [%s] is invalid.", PACKAGE_XML));
            throw e;
        }

        // Next, we scan the hole directory and subdirectories for documents.

        Queue<File> fileQueue = new LinkedList<>();
        addContentsToQueue(fileQueue, sourceDir);
        while (!fileQueue.isEmpty() && !documentNames.isEmpty()) {
            File currentFile = fileQueue.poll();
            if (currentFile.isDirectory()) {
                addContentsToQueue(fileQueue, currentFile);
            } else {
                String documentReference = XWikiDocument.getReference(currentFile);
                if (documentNames.contains(documentReference)) {
                    // building the path the current file will have within the archive
                    //
                    // Note: DO NOT USE String.split since it requires a regexp. Under Windows XP, the FileSeparator is
                    // '\' when not escaped is a special character of the regexp
                    //   String archivedFilePath =
                    //   currentFile.getAbsolutePath().split(sourceDir.getAbsolutePath() + File.separator)[1];
                    String archivedFilePath = currentFile.getAbsolutePath()
                        .substring((sourceDir.getAbsolutePath() + File.separator).length());
                    archivedFilePath = archivedFilePath.replace(File.separatorChar, '/');

                    archiver.addFile(currentFile, archivedFilePath);
                    documentNames.remove(documentReference);
                }
            }
        }

        if (!documentNames.isEmpty()) {
            StringBuilder errorMessage = new StringBuilder("The following documents could not be found: ");
            for (String name : documentNames) {
                errorMessage.append(name);
                errorMessage.append(" ");
            }
            throw new Exception(errorMessage.toString());
        }

        archiver.addFile(packageXml, PACKAGE_XML);
    }
private void addFilesToArchive ( ZipArchiver archiver , File sourceDir , File packageXml ) throws Exception { Collection < String > documentNames ; getLog ( ) . info ( String . format ( "Using the existing package.xml descriptor at [%s]" , packageXml . getPath ( ) ) ) ; try { documentNames = getDocumentNamesFromXML ( packageXml ) ; } catch ( Exception e ) { getLog ( ) . error ( String . format ( "The existing [%s] is invalid." , PACKAGE_XML ) ) ; throw e ; } Queue < File > fileQueue = new LinkedList < > ( ) ; addContentsToQueue ( fileQueue , sourceDir ) ; while ( ! fileQueue . isEmpty ( ) && ! documentNames . isEmpty ( ) ) { File currentFile = fileQueue . poll ( ) ; if ( currentFile . isDirectory ( ) ) { addContentsToQueue ( fileQueue , currentFile ) ; } else { String documentReference = XWikiDocument . getReference ( currentFile ) ; if ( documentNames . contains ( documentReference ) ) { String archivedFilePath = currentFile . getAbsolutePath ( ) . substring ( ( sourceDir . getAbsolutePath ( ) + File . separator ) . length ( ) ) ; archivedFilePath = archivedFilePath . replace ( File . separatorChar , '/' ) ; archiver . addFile ( currentFile , archivedFilePath ) ; documentNames . remove ( documentReference ) ; } } } if ( ! documentNames . isEmpty ( ) ) { StringBuilder errorMessage = new StringBuilder ( "The following documents could not be found: " ) ; for ( String name : documentNames ) { errorMessage . append ( name ) ; errorMessage . append ( " " ) ; } throw new Exception ( errorMessage . toString ( ) ) ; } archiver . addFile ( packageXml , PACKAGE_XML ) ; }
Adds files from a specific directory to an archive. It uses an existing package.xml to filter the files to be
added.

@param archiver the archive in which the files will be added
@param sourceDir the directory whose contents will be added to the archive
@param packageXml the corresponding package.xml file
@throws Exception if the files cannot be added to the archive