Example usage for java.util.zip ZipOutputStream finish

List of usage examples for java.util.zip ZipOutputStream finish

Introduction

In this page you can find the example usage for java.util.zip ZipOutputStream finish.

Prototype

public void finish() throws IOException 

Source Link

Document

Finishes writing the contents of the ZIP output stream without closing the underlying stream.

Usage

From source file:com.panet.imeta.trans.steps.mail.Mail.java

private void setAttachedFilesList(Object[] r, LogWriter log) throws Exception {
    String realSourceFileFoldername = null;
    String realSourceWildcard = null;
    FileObject sourcefile = null;
    FileObject file = null;/*from   w w  w  . j av  a2 s.c  om*/

    ZipOutputStream zipOutputStream = null;
    File masterZipfile = null;

    if (meta.isZipFilenameDynamic())
        data.ZipFilename = data.previousRowMeta.getString(r, data.indexOfDynamicZipFilename);

    try {

        if (meta.isDynamicFilename()) {
            // dynamic attached filenames
            if (data.indexOfSourceFilename > -1)
                realSourceFileFoldername = data.previousRowMeta.getString(r, data.indexOfSourceFilename);

            if (data.indexOfSourceWildcard > -1)
                realSourceWildcard = data.previousRowMeta.getString(r, data.indexOfSourceWildcard);

        } else {
            // static attached filenames
            realSourceFileFoldername = data.realSourceFileFoldername;
            realSourceWildcard = data.realSourceWildcard;
        }

        if (!Const.isEmpty(realSourceFileFoldername)) {
            sourcefile = KettleVFS.getFileObject(realSourceFileFoldername);
            if (sourcefile.exists()) {
                long FileSize = 0;
                FileObject list[] = null;
                if (sourcefile.getType() == FileType.FILE) {
                    list = new FileObject[1];
                    list[0] = sourcefile;
                } else
                    list = sourcefile
                            .findFiles(new TextFileSelector(sourcefile.toString(), realSourceWildcard));
                if (list.length > 0) {

                    boolean zipFiles = meta.isZipFiles();
                    if (zipFiles && data.zipFileLimit == 0) {
                        masterZipfile = new File(
                                System.getProperty("java.io.tmpdir") + Const.FILE_SEPARATOR + data.ZipFilename);

                        zipOutputStream = new ZipOutputStream(new FileOutputStream(masterZipfile));
                    }

                    for (int i = 0; i < list.length; i++) {

                        file = KettleVFS.getFileObject(KettleVFS.getFilename(list[i]));

                        if (zipFiles) {

                            if (data.zipFileLimit == 0) {
                                ZipEntry zipEntry = new ZipEntry(file.getName().getBaseName());
                                zipOutputStream.putNextEntry(zipEntry);

                                // Now put the content of this file into this archive...
                                BufferedInputStream inputStream = new BufferedInputStream(
                                        file.getContent().getInputStream());
                                int c;
                                while ((c = inputStream.read()) >= 0) {
                                    zipOutputStream.write(c);
                                }
                                inputStream.close();
                                zipOutputStream.closeEntry();
                            } else
                                FileSize += file.getContent().getSize();
                        } else {
                            addAttachedFilePart(file);
                        }
                    } // end for
                    if (zipFiles) {
                        if (log.isDebug())
                            log.logDebug(toString(), Messages.getString("Mail.Log.FileSize", "" + FileSize));
                        if (log.isDebug())
                            log.logDebug(toString(),
                                    Messages.getString("Mail.Log.LimitSize", "" + data.zipFileLimit));

                        if (data.zipFileLimit > 0 && FileSize > data.zipFileLimit) {

                            masterZipfile = new File(System.getProperty("java.io.tmpdir") + Const.FILE_SEPARATOR
                                    + data.ZipFilename);

                            zipOutputStream = new ZipOutputStream(new FileOutputStream(masterZipfile));

                            for (int i = 0; i < list.length; i++) {

                                file = KettleVFS.getFileObject(KettleVFS.getFilename(list[i]));

                                ZipEntry zipEntry = new ZipEntry(file.getName().getBaseName());
                                zipOutputStream.putNextEntry(zipEntry);

                                // Now put the content of this file into this archive...
                                BufferedInputStream inputStream = new BufferedInputStream(
                                        file.getContent().getInputStream());
                                int c;
                                while ((c = inputStream.read()) >= 0) {
                                    zipOutputStream.write(c);
                                }
                                inputStream.close();
                                zipOutputStream.closeEntry();

                            }

                        }
                        if (data.zipFileLimit > 0 && FileSize > data.zipFileLimit || data.zipFileLimit == 0) {
                            file = KettleVFS.getFileObject(masterZipfile.getAbsolutePath());
                            addAttachedFilePart(file);
                        }
                    }
                }
            } else {
                log.logError(toString(),
                        Messages.getString("Mail.Error.SourceFileFolderNotExists", realSourceFileFoldername));
            }
        }
    } catch (Exception e) {
        log.logError(toString(), e.getMessage());
    } finally {
        if (sourcefile != null) {
            try {
                sourcefile.close();
            } catch (Exception e) {
            }
        }
        if (file != null) {
            try {
                file.close();
            } catch (Exception e) {
            }
        }

        if (zipOutputStream != null) {
            try {
                zipOutputStream.finish();
                zipOutputStream.close();
            } catch (IOException e) {
                log.logError(toString(), "Unable to close attachement zip file archive : " + e.toString());
            }
        }
    }

}

From source file:org.pentaho.di.trans.steps.mail.Mail.java

private void setAttachedFilesList(Object[] r, LogChannelInterface log) throws Exception {
    String realSourceFileFoldername = null;
    String realSourceWildcard = null;
    FileObject sourcefile = null;
    FileObject file = null;/* w  ww.jav a 2 s. c  o m*/

    ZipOutputStream zipOutputStream = null;
    File masterZipfile = null;

    if (meta.isZipFilenameDynamic()) {
        data.ZipFilename = data.previousRowMeta.getString(r, data.indexOfDynamicZipFilename);
    }

    try {

        if (meta.isDynamicFilename()) {
            // dynamic attached filenames
            if (data.indexOfSourceFilename > -1) {
                realSourceFileFoldername = data.previousRowMeta.getString(r, data.indexOfSourceFilename);
            }

            if (data.indexOfSourceWildcard > -1) {
                realSourceWildcard = data.previousRowMeta.getString(r, data.indexOfSourceWildcard);
            }

        } else {
            // static attached filenames
            realSourceFileFoldername = data.realSourceFileFoldername;
            realSourceWildcard = data.realSourceWildcard;
        }

        if (!Const.isEmpty(realSourceFileFoldername)) {
            sourcefile = KettleVFS.getFileObject(realSourceFileFoldername, getTransMeta());
            if (sourcefile.exists()) {
                long FileSize = 0;
                FileObject[] list = null;
                if (sourcefile.getType() == FileType.FILE) {
                    list = new FileObject[1];
                    list[0] = sourcefile;
                } else {
                    list = sourcefile
                            .findFiles(new TextFileSelector(sourcefile.toString(), realSourceWildcard));
                }
                if (list.length > 0) {

                    boolean zipFiles = meta.isZipFiles();
                    if (zipFiles && data.zipFileLimit == 0) {
                        masterZipfile = new File(
                                System.getProperty("java.io.tmpdir") + Const.FILE_SEPARATOR + data.ZipFilename);

                        zipOutputStream = new ZipOutputStream(new FileOutputStream(masterZipfile));
                    }

                    for (int i = 0; i < list.length; i++) {

                        file = KettleVFS.getFileObject(KettleVFS.getFilename(list[i]), getTransMeta());

                        if (zipFiles) {

                            if (data.zipFileLimit == 0) {
                                ZipEntry zipEntry = new ZipEntry(file.getName().getBaseName());
                                zipOutputStream.putNextEntry(zipEntry);

                                // Now put the content of this file into this archive...
                                BufferedInputStream inputStream = new BufferedInputStream(
                                        file.getContent().getInputStream());
                                int c;
                                while ((c = inputStream.read()) >= 0) {
                                    zipOutputStream.write(c);
                                }
                                inputStream.close();
                                zipOutputStream.closeEntry();
                            } else {
                                FileSize += file.getContent().getSize();
                            }
                        } else {
                            addAttachedFilePart(file);
                        }
                    } // end for
                    if (zipFiles) {
                        if (isDebug()) {
                            logDebug(BaseMessages.getString(PKG, "Mail.Log.FileSize", "" + FileSize));
                        }
                        if (isDebug()) {
                            logDebug(BaseMessages.getString(PKG, "Mail.Log.LimitSize", "" + data.zipFileLimit));
                        }

                        if (data.zipFileLimit > 0 && FileSize > data.zipFileLimit) {

                            masterZipfile = new File(System.getProperty("java.io.tmpdir") + Const.FILE_SEPARATOR
                                    + data.ZipFilename);

                            zipOutputStream = new ZipOutputStream(new FileOutputStream(masterZipfile));

                            for (int i = 0; i < list.length; i++) {

                                file = KettleVFS.getFileObject(KettleVFS.getFilename(list[i]), getTransMeta());

                                ZipEntry zipEntry = new ZipEntry(file.getName().getBaseName());
                                zipOutputStream.putNextEntry(zipEntry);

                                // Now put the content of this file into this archive...
                                BufferedInputStream inputStream = new BufferedInputStream(
                                        file.getContent().getInputStream());
                                int c;
                                while ((c = inputStream.read()) >= 0) {
                                    zipOutputStream.write(c);
                                }
                                inputStream.close();
                                zipOutputStream.closeEntry();

                            }

                        }
                        if (data.zipFileLimit > 0 && FileSize > data.zipFileLimit || data.zipFileLimit == 0) {
                            file = KettleVFS.getFileObject(masterZipfile.getAbsolutePath(), getTransMeta());
                            addAttachedFilePart(file);
                        }
                    }
                }
            } else {
                logError(BaseMessages.getString(PKG, "Mail.Error.SourceFileFolderNotExists",
                        realSourceFileFoldername));
            }
        }
    } catch (Exception e) {
        logError(e.getMessage());
    } finally {
        if (sourcefile != null) {
            try {
                sourcefile.close();
            } catch (Exception e) {
                // Ignore errors
            }
        }
        if (file != null) {
            try {
                file.close();
            } catch (Exception e) {
                // Ignore errors
            }
        }

        if (zipOutputStream != null) {
            try {
                zipOutputStream.finish();
                zipOutputStream.close();
            } catch (IOException e) {
                logError("Unable to close attachement zip file archive : " + e.toString());
            }
        }
    }

}

From source file:org.sakaiproject.assignment.impl.AssignmentServiceImpl.java

protected void zipGroupSubmissions(String assignmentReference, String assignmentTitle, String gradeTypeString,
        Assignment.SubmissionType typeOfSubmission, Iterator submissions, OutputStream outputStream,
        StringBuilder exceptionMessage, boolean withStudentSubmissionText,
        boolean withStudentSubmissionAttachment, boolean withGradeFile, boolean withFeedbackText,
        boolean withFeedbackComment, boolean withFeedbackAttachment, String gradeFileFormat,
        boolean includeNotSubmitted) {
    ZipOutputStream out = null;
    try {/*from   w ww  . j a  v  a 2s . co  m*/
        out = new ZipOutputStream(outputStream);

        // create the folder structure - named after the assignment's title
        final String root = escapeInvalidCharsEntry(Validator.escapeZipEntry(assignmentTitle))
                + Entity.SEPARATOR;

        final SpreadsheetExporter.Type type = SpreadsheetExporter.Type.valueOf(gradeFileFormat.toUpperCase());
        final SpreadsheetExporter sheet = SpreadsheetExporter.getInstance(type, assignmentTitle,
                gradeTypeString, getCsvSeparator());

        if (!submissions.hasNext()) {
            exceptionMessage.append("There is no submission yet. ");
        }

        // Write the header
        sheet.addHeader(resourceLoader.getString("group"), resourceLoader.getString("grades.eid"),
                resourceLoader.getString("grades.members"), resourceLoader.getString("grades.grade"),
                resourceLoader.getString("grades.submissionTime"), resourceLoader.getString("grades.late"));

        // allow add assignment members
        allowAddSubmissionUsers(assignmentReference);

        // Create the ZIP file
        String caughtException = null;
        String caughtStackTrace = null;
        while (submissions.hasNext()) {
            final AssignmentSubmission s = (AssignmentSubmission) submissions.next();

            log.debug(this + " ZIPGROUP " + (s == null ? "null" : s.getId()));

            //SAK-29314 added a new value where it's by default submitted but is marked when the user submits
            if ((s.getSubmitted() && s.getUserSubmission()) || includeNotSubmitted) {
                try {
                    final StringBuilder submittersName = new StringBuilder(root);

                    final User[] submitters = s.getSubmitters().stream().map(p -> {
                        try {
                            return userDirectoryService.getUser(p.getSubmitter());
                        } catch (UserNotDefinedException e) {
                            log.warn("User not found {}", p.getSubmitter());
                            return null;
                        }
                    }).filter(Objects::nonNull).toArray(User[]::new);

                    final String groupTitle = siteService.getSite(s.getAssignment().getContext())
                            .getGroup(s.getGroupId()).getTitle();
                    final StringBuilder submittersString = new StringBuilder();
                    final StringBuilder submitters2String = new StringBuilder();

                    for (int i = 0; i < submitters.length; i++) {
                        if (i > 0) {
                            submittersString.append("; ");
                            submitters2String.append("; ");
                        }
                        String fullName = submitters[i].getSortName();
                        // in case the user doesn't have first name or last name
                        if (fullName.indexOf(",") == -1) {
                            fullName = fullName.concat(",");
                        }
                        submittersString.append(fullName);
                        submitters2String.append(submitters[i].getDisplayName());
                        // add the eid to the end of it to guarantee folder name uniqness
                        submittersString.append("(" + submitters[i].getEid() + ")");
                    }
                    final String latenessStatus = whenSubmissionMade(s);

                    final String gradeDisplay = getGradeDisplay(s.getGrade(),
                            s.getAssignment().getTypeOfGrade(), s.getAssignment().getScaleFactor());

                    //Adding the row
                    sheet.addRow(groupTitle, s.getGroupId(), submitters2String.toString(), gradeDisplay,
                            s.getDateSubmitted() != null ? s.getDateSubmitted().toString() : StringUtils.EMPTY,
                            latenessStatus);

                    if (StringUtils.trimToNull(groupTitle) != null) {
                        submittersName.append(StringUtils.trimToNull(groupTitle)).append(" (")
                                .append(s.getGroupId()).append(")");
                        final String submittedText = s.getSubmittedText();

                        submittersName.append("/");

                        // record submission timestamp
                        if (s.getSubmitted() && s.getDateSubmitted() != null) {
                            createTextZipEntry(out, submittersName + "timestamp.txt",
                                    s.getDateSubmitted().toString());
                        }

                        // create the folder structure - named after the submitter's name
                        if (typeOfSubmission != Assignment.SubmissionType.ATTACHMENT_ONLY_ASSIGNMENT_SUBMISSION
                                && typeOfSubmission != Assignment.SubmissionType.NON_ELECTRONIC_ASSIGNMENT_SUBMISSION) {
                            // include student submission text
                            if (withStudentSubmissionText) {
                                // create the text file only when a text submission is allowed
                                final String zipEntryName = submittersName + groupTitle + "_submissionText"
                                        + AssignmentConstants.ZIP_SUBMITTED_TEXT_FILE_TYPE;
                                createTextZipEntry(out, zipEntryName, submittedText);
                            }

                            // include student submission feedback text
                            if (withFeedbackText) {
                                // create a feedbackText file into zip
                                createTextZipEntry(out, submittersName + "feedbackText.html",
                                        s.getFeedbackText());
                            }
                        }

                        if (typeOfSubmission != Assignment.SubmissionType.TEXT_ONLY_ASSIGNMENT_SUBMISSION
                                && typeOfSubmission != Assignment.SubmissionType.NON_ELECTRONIC_ASSIGNMENT_SUBMISSION
                                && withStudentSubmissionAttachment) {
                            // include student submission attachment
                            // create a attachment folder for the submission attachments
                            final String sSubAttachmentFolder = submittersName
                                    + resourceLoader.getString("stuviewsubm.submissatt") + "/";
                            final ZipEntry sSubAttachmentFolderEntry = new ZipEntry(sSubAttachmentFolder);
                            out.putNextEntry(sSubAttachmentFolderEntry);
                            // add all submission attachment into the submission attachment folder
                            zipAttachments(out, submittersName.toString(), sSubAttachmentFolder,
                                    s.getAttachments());
                            out.closeEntry();
                        }

                        if (withFeedbackComment) {
                            // the comments.txt file to show instructor's comments
                            final String zipEntryName = submittersName + "comments"
                                    + AssignmentConstants.ZIP_COMMENT_FILE_TYPE;
                            final String textEntryString = formattedText.encodeUnicode(s.getFeedbackComment());
                            createTextZipEntry(out, zipEntryName, textEntryString);
                        }

                        if (withFeedbackAttachment) {
                            // create an attachment folder for the feedback attachments
                            final String feedbackSubAttachmentFolder = submittersName
                                    + resourceLoader.getString("download.feedback.attachment") + "/";
                            final ZipEntry feedbackSubAttachmentFolderEntry = new ZipEntry(
                                    feedbackSubAttachmentFolder);
                            out.putNextEntry(feedbackSubAttachmentFolderEntry);
                            // add all feedback attachment folder
                            zipAttachments(out, submittersName.toString(), feedbackSubAttachmentFolder,
                                    s.getFeedbackAttachments());
                            out.closeEntry();
                        }

                        if (!submittersString.toString().trim().isEmpty()) {
                            // the comments.txt file to show instructor's comments
                            final String zipEntryName = submittersName + "members"
                                    + AssignmentConstants.ZIP_COMMENT_FILE_TYPE;
                            final String textEntryString = formattedText
                                    .encodeUnicode(submittersString.toString());
                            createTextZipEntry(out, zipEntryName, textEntryString);
                        }

                    } // if
                } catch (Exception e) {
                    caughtException = e.toString();
                    if (log.isDebugEnabled()) {
                        caughtStackTrace = ExceptionUtils.getStackTrace(e);
                    }
                    break;
                }
            } // if the user is still in site

        } // while -- there is submission

        if (caughtException == null) {
            // continue
            if (withGradeFile) {
                final ZipEntry gradesCSVEntry = new ZipEntry(root + "grades." + sheet.getFileExtension());
                out.putNextEntry(gradesCSVEntry);
                sheet.write(out);
                out.closeEntry();
            }
        } else {
            // log the error
            exceptionMessage.append(" Exception " + caughtException
                    + " for creating submission zip file for assignment " + "\"" + assignmentTitle + "\"\n");
            if (log.isDebugEnabled()) {
                exceptionMessage.append(caughtStackTrace);
            }
        }
    } catch (IOException e) {
        exceptionMessage.append("IOException for creating submission zip file for assignment " + "\""
                + assignmentTitle + "\" exception: " + e + "\n");
    } finally {
        // Complete the ZIP file
        if (out != null) {
            try {
                out.finish();
                out.flush();
            } catch (IOException e) {
                // tried
            }
            try {
                out.close();
            } catch (IOException e) {
                // tried
            }
        }
    }
}

From source file:org.sakaiproject.assignment.impl.BaseAssignmentService.java

protected void zipGroupSubmissions(String assignmentReference, String assignmentTitle, String gradeTypeString,
        int typeOfSubmission, Iterator submissions, OutputStream outputStream, StringBuilder exceptionMessage,
        boolean withStudentSubmissionText, boolean withStudentSubmissionAttachment, boolean withGradeFile,
        boolean withFeedbackText, boolean withFeedbackComment, boolean withFeedbackAttachment,
        String gradeFileFormat, boolean includeNotSubmitted) {
    ZipOutputStream out = null;
    try {//from  w w w. j ava2 s. c om
        out = new ZipOutputStream(outputStream);

        // create the folder structure - named after the assignment's title
        String root = escapeInvalidCharsEntry(Validator.escapeZipEntry(assignmentTitle)) + Entity.SEPARATOR;

        SpreadsheetExporter.Type type = SpreadsheetExporter.Type.valueOf(gradeFileFormat.toUpperCase());
        SpreadsheetExporter sheet = SpreadsheetExporter.getInstance(type, assignmentTitle, gradeTypeString);

        String submittedText = "";
        if (!submissions.hasNext()) {
            exceptionMessage.append("There is no submission yet. ");
        }

        // Write the header
        sheet.addHeader("Group", rb.getString("grades.eid"), rb.getString("grades.members"),
                rb.getString("grades.grade"), rb.getString("grades.submissionTime"),
                rb.getString("grades.late"));

        // allow add assignment members
        List allowAddSubmissionUsers = allowAddSubmissionUsers(assignmentReference);

        // Create the ZIP file
        String submittersName = "";
        String caughtException = null;
        String caughtStackTrace = null;
        while (submissions.hasNext()) {

            GroupSubmission gs = (GroupSubmission) submissions.next();
            AssignmentSubmission s = gs.getSubmission();

            M_log.debug(this + " ZIPGROUP " + (s == null ? "null" : s.getId()));

            //SAK-29314 added a new value where it's by default submitted but is marked when the user submits
            if ((s.getSubmitted() && s.isUserSubmission()) || includeNotSubmitted) {
                try {
                    submittersName = root;

                    User[] submitters = s.getSubmitters();
                    String submitterString = gs.getGroup().getTitle() + " (" + gs.getGroup().getId() + ")";
                    String submittersString = "";
                    String submitters2String = "";

                    for (int i = 0; i < submitters.length; i++) {
                        if (i > 0) {
                            submittersString = submittersString.concat("; ");
                            submitters2String = submitters2String.concat("; ");
                        }
                        String fullName = submitters[i].getSortName();
                        // in case the user doesn't have first name or last name
                        if (fullName.indexOf(",") == -1) {
                            fullName = fullName.concat(",");
                        }
                        submittersString = submittersString.concat(fullName);
                        submitters2String = submitters2String.concat(submitters[i].getDisplayName());
                        // add the eid to the end of it to guarantee folder name uniqness
                        submittersString = submittersString + "(" + submitters[i].getEid() + ")";
                    }
                    String latenessStatus = whenSubmissionMade(s);

                    //Adding the row
                    sheet.addRow(gs.getGroup().getTitle(), gs.getGroup().getId(), submitters2String,
                            s.getGradeDisplay(), s.getTimeSubmittedString(), latenessStatus);

                    if (StringUtil.trimToNull(submitterString) != null) {
                        submittersName = submittersName.concat(StringUtil.trimToNull(submitterString));
                        submittedText = s.getSubmittedText();

                        submittersName = submittersName.concat("/");

                        // record submission timestamp
                        if (s.getSubmitted() && s.getTimeSubmitted() != null) {
                            ZipEntry textEntry = new ZipEntry(submittersName + "timestamp.txt");
                            out.putNextEntry(textEntry);
                            byte[] b = (s.getTimeSubmitted().toString()).getBytes();
                            out.write(b);
                            textEntry.setSize(b.length);
                            out.closeEntry();
                        }

                        // create the folder structure - named after the submitter's name
                        if (typeOfSubmission != Assignment.ATTACHMENT_ONLY_ASSIGNMENT_SUBMISSION
                                && typeOfSubmission != Assignment.NON_ELECTRONIC_ASSIGNMENT_SUBMISSION) {
                            // include student submission text
                            if (withStudentSubmissionText) {
                                // create the text file only when a text submission is allowed
                                ZipEntry textEntry = new ZipEntry(submittersName + submitterString
                                        + "_submissionText" + ZIP_SUBMITTED_TEXT_FILE_TYPE);
                                out.putNextEntry(textEntry);
                                byte[] text = submittedText.getBytes();
                                out.write(text);
                                textEntry.setSize(text.length);
                                out.closeEntry();
                            }

                            // include student submission feedback text
                            if (withFeedbackText) {
                                // create a feedbackText file into zip
                                ZipEntry fTextEntry = new ZipEntry(submittersName + "feedbackText.html");
                                out.putNextEntry(fTextEntry);
                                byte[] fText = s.getFeedbackText().getBytes();
                                out.write(fText);
                                fTextEntry.setSize(fText.length);
                                out.closeEntry();
                            }
                        }

                        if (typeOfSubmission != Assignment.TEXT_ONLY_ASSIGNMENT_SUBMISSION
                                && typeOfSubmission != Assignment.NON_ELECTRONIC_ASSIGNMENT_SUBMISSION) {
                            // include student submission attachment
                            if (withStudentSubmissionAttachment) {
                                // create a attachment folder for the submission attachments
                                String sSubAttachmentFolder = submittersName
                                        + rb.getString("stuviewsubm.submissatt") + "/";
                                ZipEntry sSubAttachmentFolderEntry = new ZipEntry(sSubAttachmentFolder);
                                out.putNextEntry(sSubAttachmentFolderEntry);
                                // add all submission attachment into the submission attachment folder
                                zipAttachments(out, submittersName, sSubAttachmentFolder,
                                        s.getSubmittedAttachments());
                                out.closeEntry();
                            }
                        }

                        if (withFeedbackComment) {
                            // the comments.txt file to show instructor's comments
                            ZipEntry textEntry = new ZipEntry(
                                    submittersName + "comments" + ZIP_COMMENT_FILE_TYPE);
                            out.putNextEntry(textEntry);
                            byte[] b = FormattedText.encodeUnicode(s.getFeedbackComment()).getBytes();
                            out.write(b);
                            textEntry.setSize(b.length);
                            out.closeEntry();
                        }

                        if (withFeedbackAttachment) {
                            // create an attachment folder for the feedback attachments
                            String feedbackSubAttachmentFolder = submittersName
                                    + rb.getString("download.feedback.attachment") + "/";
                            ZipEntry feedbackSubAttachmentFolderEntry = new ZipEntry(
                                    feedbackSubAttachmentFolder);
                            out.putNextEntry(feedbackSubAttachmentFolderEntry);
                            // add all feedback attachment folder
                            zipAttachments(out, submittersName, feedbackSubAttachmentFolder,
                                    s.getFeedbackAttachments());
                            out.closeEntry();
                        }

                        if (submittersString.trim().length() > 0) {
                            // the comments.txt file to show instructor's comments
                            ZipEntry textEntry = new ZipEntry(
                                    submittersName + "members" + ZIP_COMMENT_FILE_TYPE);
                            out.putNextEntry(textEntry);
                            byte[] b = FormattedText.encodeUnicode(submittersString).getBytes();
                            out.write(b);
                            textEntry.setSize(b.length);
                            out.closeEntry();
                        }

                    } // if
                } catch (Exception e) {
                    caughtException = e.toString();
                    if (M_log.isDebugEnabled()) {
                        caughtStackTrace = ExceptionUtils.getStackTrace(e);
                    }
                    break;
                }
            } // if the user is still in site

        } // while -- there is submission

        if (caughtException == null) {
            // continue
            if (withGradeFile) {
                ZipEntry gradesCSVEntry = new ZipEntry(root + "grades." + sheet.getFileExtension());
                out.putNextEntry(gradesCSVEntry);
                sheet.write(out);
                out.closeEntry();
            }
        } else {
            // log the error
            exceptionMessage.append(" Exception " + caughtException
                    + " for creating submission zip file for assignment " + "\"" + assignmentTitle + "\"\n");
            if (M_log.isDebugEnabled()) {
                exceptionMessage.append(caughtStackTrace);
            }
        }
    } catch (IOException e) {
        exceptionMessage.append("IOException for creating submission zip file for assignment " + "\""
                + assignmentTitle + "\" exception: " + e + "\n");
    } finally {
        // Complete the ZIP file
        if (out != null) {
            try {
                out.finish();
                out.flush();
            } catch (IOException e) {
                // tried
            }
            try {
                out.close();
            } catch (IOException e) {
                // tried
            }
        }
    }
}

From source file:org.sakaiproject.assignment.impl.AssignmentServiceImpl.java

private void zipSubmissions(String assignmentReference, String assignmentTitle, Assignment.GradeType gradeType,
        Assignment.SubmissionType typeOfSubmission, Iterator submissions, OutputStream outputStream,
        StringBuilder exceptionMessage, boolean withStudentSubmissionText,
        boolean withStudentSubmissionAttachment, boolean withGradeFile, boolean withFeedbackText,
        boolean withFeedbackComment, boolean withFeedbackAttachment, boolean withoutFolders,
        String gradeFileFormat, boolean includeNotSubmitted, String siteId) {
    ZipOutputStream out = null;

    boolean isAdditionalNotesEnabled = false;
    Site st = null;//from w ww .j a  v  a 2s . c o m
    try {
        st = siteService.getSite(siteId);
        isAdditionalNotesEnabled = candidateDetailProvider != null
                && candidateDetailProvider.isAdditionalNotesEnabled(st);
    } catch (IdUnusedException e) {
        log.warn("Could not find site {} - isAdditionalNotesEnabled set to false", siteId);
    }

    try {
        out = new ZipOutputStream(outputStream);

        // create the folder structure - named after the assignment's title
        final String root = escapeInvalidCharsEntry(Validator.escapeZipEntry(assignmentTitle))
                + Entity.SEPARATOR;

        final SpreadsheetExporter.Type type = SpreadsheetExporter.Type.valueOf(gradeFileFormat.toUpperCase());
        final SpreadsheetExporter sheet = SpreadsheetExporter.getInstance(type, assignmentTitle,
                gradeType.toString(), getCsvSeparator());

        String submittedText = "";
        if (!submissions.hasNext()) {
            exceptionMessage.append("There is no submission yet. ");
        }

        if (isAdditionalNotesEnabled) {
            sheet.addHeader(resourceLoader.getString("grades.id"), resourceLoader.getString("grades.eid"),
                    resourceLoader.getString("grades.lastname"), resourceLoader.getString("grades.firstname"),
                    resourceLoader.getString("grades.grade"), resourceLoader.getString("grades.submissionTime"),
                    resourceLoader.getString("grades.late"), resourceLoader.getString("gen.notes"));
        } else {
            sheet.addHeader(resourceLoader.getString("grades.id"), resourceLoader.getString("grades.eid"),
                    resourceLoader.getString("grades.lastname"), resourceLoader.getString("grades.firstname"),
                    resourceLoader.getString("grades.grade"), resourceLoader.getString("grades.submissionTime"),
                    resourceLoader.getString("grades.late"));
        }

        // allow add assignment members
        final List<User> allowAddSubmissionUsers = allowAddSubmissionUsers(assignmentReference);

        // Create the ZIP file
        String caughtException = null;
        String caughtStackTrace = null;
        final StringBuilder submittersAdditionalNotesHtml = new StringBuilder();

        while (submissions.hasNext()) {
            final AssignmentSubmission s = (AssignmentSubmission) submissions.next();
            boolean isAnon = assignmentUsesAnonymousGrading(s.getAssignment());
            //SAK-29314 added a new value where it's by default submitted but is marked when the user submits
            if ((s.getSubmitted() && s.getUserSubmission()) || includeNotSubmitted) {
                // get the submitter who submitted the submission see if the user is still in site
                final Optional<AssignmentSubmissionSubmitter> assignmentSubmitter = s.getSubmitters().stream()
                        .findAny();
                try {
                    User u = null;
                    if (assignmentSubmitter.isPresent()) {
                        u = userDirectoryService.getUser(assignmentSubmitter.get().getSubmitter());
                    }
                    if (allowAddSubmissionUsers.contains(u)) {
                        String submittersName = root;

                        final User[] submitters = s.getSubmitters().stream().map(p -> {
                            try {
                                return userDirectoryService.getUser(p.getSubmitter());
                            } catch (UserNotDefinedException e) {
                                log.warn("User not found {}, {}", p.getSubmitter(), e.getMessage());
                            }
                            return null;
                        }).filter(Objects::nonNull).toArray(User[]::new);

                        String submittersString = "";
                        for (int i = 0; i < submitters.length; i++) {
                            if (i > 0) {
                                submittersString = submittersString.concat("; ");
                            }
                            String fullName = submitters[i].getSortName();
                            // in case the user doesn't have first name or last name
                            if (!fullName.contains(",")) {
                                fullName = fullName.concat(",");
                            }
                            submittersString = submittersString.concat(fullName);
                            // add the eid to the end of it to guarantee folder name uniqness
                            // if user Eid contains non ascii characters, the user internal id will be used
                            final String userEid = submitters[i].getEid();
                            final String candidateEid = escapeInvalidCharsEntry(userEid);
                            if (candidateEid.equals(userEid)) {
                                submittersString = submittersString + "(" + candidateEid + ")";
                            } else {
                                submittersString = submittersString + "(" + submitters[i].getId() + ")";
                            }
                            submittersString = escapeInvalidCharsEntry(submittersString);
                            // Work out if submission is late.
                            final String latenessStatus = whenSubmissionMade(s);
                            log.debug("latenessStatus: " + latenessStatus);

                            final String anonTitle = resourceLoader.getString("grading.anonymous.title");
                            final String fullAnonId = s.getId() + " " + anonTitle;

                            String[] params = new String[7];
                            if (isAdditionalNotesEnabled && candidateDetailProvider != null) {
                                final List<String> notes = candidateDetailProvider
                                        .getAdditionalNotes(submitters[i], st).orElse(new ArrayList<String>());

                                if (!notes.isEmpty()) {
                                    params = new String[notes.size() + 7];
                                    System.arraycopy(notes.toArray(new String[notes.size()]), 0, params, 7,
                                            notes.size());
                                }
                            }

                            // SAK-17606
                            if (!isAnon) {
                                log.debug("Zip user: " + submitters[i].toString());
                                params[0] = submitters[i].getDisplayId();
                                params[1] = submitters[i].getEid();
                                params[2] = submitters[i].getLastName();
                                params[3] = submitters[i].getFirstName();
                                params[4] = this.getGradeForSubmitter(s, submitters[i].getId());
                                if (s.getDateSubmitted() != null) {
                                    params[5] = s.getDateSubmitted().toString(); // TODO may need to be formatted
                                } else {
                                    params[5] = "";
                                }
                                params[6] = latenessStatus;
                            } else {
                                params[0] = fullAnonId;
                                params[1] = fullAnonId;
                                params[2] = anonTitle;
                                params[3] = anonTitle;
                                params[4] = this.getGradeForSubmitter(s, submitters[i].getId());
                                if (s.getDateSubmitted() != null) {
                                    params[5] = s.getDateSubmitted().toString(); // TODO may need to be formatted
                                } else {
                                    params[5] = "";
                                }
                                params[6] = latenessStatus;
                            }
                            sheet.addRow(params);
                        }

                        if (StringUtils.trimToNull(submittersString) != null) {
                            submittersName = submittersName.concat(StringUtils.trimToNull(submittersString));
                            submittedText = s.getSubmittedText();

                            // SAK-17606
                            if (isAnon) {
                                submittersString = s.getId() + " "
                                        + resourceLoader.getString("grading.anonymous.title");
                                submittersName = root + submittersString;
                            }

                            if (!withoutFolders) {
                                submittersName = submittersName.concat("/");
                            } else {
                                submittersName = submittersName.concat("_");
                            }

                            // record submission timestamp
                            if (!withoutFolders && s.getSubmitted() && s.getDateSubmitted() != null) {
                                final String zipEntryName = submittersName + "timestamp.txt";
                                final String textEntryString = s.getDateSubmitted().toString();
                                createTextZipEntry(out, zipEntryName, textEntryString);
                            }

                            // create the folder structure - named after the submitter's name
                            if (typeOfSubmission != Assignment.SubmissionType.ATTACHMENT_ONLY_ASSIGNMENT_SUBMISSION
                                    && typeOfSubmission != Assignment.SubmissionType.NON_ELECTRONIC_ASSIGNMENT_SUBMISSION) {
                                // include student submission text
                                if (withStudentSubmissionText) {
                                    // create the text file only when a text submission is allowed
                                    final StringBuilder submittersNameString = new StringBuilder(
                                            submittersName);
                                    //remove folder name if Download All is without user folders
                                    if (!withoutFolders) {
                                        submittersNameString.append(submittersString);
                                    }

                                    final String zipEntryName = submittersNameString
                                            .append("_submissionText"
                                                    + AssignmentConstants.ZIP_SUBMITTED_TEXT_FILE_TYPE)
                                            .toString();
                                    createTextZipEntry(out, zipEntryName, submittedText);
                                }

                                // include student submission feedback text
                                if (withFeedbackText) {
                                    // create a feedbackText file into zip
                                    final String zipEntryName = submittersName + "feedbackText.html";
                                    final String textEntryString = s.getFeedbackText();
                                    createTextZipEntry(out, zipEntryName, textEntryString);
                                }
                            }

                            if (typeOfSubmission != Assignment.SubmissionType.TEXT_ONLY_ASSIGNMENT_SUBMISSION
                                    && typeOfSubmission != Assignment.SubmissionType.NON_ELECTRONIC_ASSIGNMENT_SUBMISSION
                                    && withStudentSubmissionAttachment) {
                                // include student submission attachment
                                //remove "/" that creates a folder if Download All is without user folders
                                String sSubAttachmentFolder = submittersName
                                        + resourceLoader.getString("stuviewsubm.submissatt");//jh + "/";
                                if (!withoutFolders) {
                                    // create a attachment folder for the submission attachments
                                    sSubAttachmentFolder = submittersName
                                            + resourceLoader.getString("stuviewsubm.submissatt") + "/";
                                    sSubAttachmentFolder = escapeInvalidCharsEntry(sSubAttachmentFolder);
                                    final ZipEntry sSubAttachmentFolderEntry = new ZipEntry(
                                            sSubAttachmentFolder);
                                    out.putNextEntry(sSubAttachmentFolderEntry);
                                } else {
                                    sSubAttachmentFolder += "_";
                                    //submittersName = submittersName.concat("_");
                                }

                                // add all submission attachment into the submission attachment folder
                                zipAttachments(out, submittersName, sSubAttachmentFolder, s.getAttachments());
                                out.closeEntry();
                            }

                            if (withFeedbackComment) {
                                // the comments.txt file to show instructor's comments
                                final String zipEntryName = submittersName + "comments"
                                        + AssignmentConstants.ZIP_COMMENT_FILE_TYPE;
                                final String textEntryString = formattedText
                                        .encodeUnicode(s.getFeedbackComment());
                                createTextZipEntry(out, zipEntryName, textEntryString);
                            }

                            if (withFeedbackAttachment) {
                                // create an attachment folder for the feedback attachments
                                String feedbackSubAttachmentFolder = submittersName
                                        + resourceLoader.getString("download.feedback.attachment");
                                if (!withoutFolders) {
                                    feedbackSubAttachmentFolder += "/";
                                    final ZipEntry feedbackSubAttachmentFolderEntry = new ZipEntry(
                                            feedbackSubAttachmentFolder);
                                    out.putNextEntry(feedbackSubAttachmentFolderEntry);
                                } else {
                                    submittersName = submittersName.concat("_");
                                }

                                // add all feedback attachment folder
                                zipAttachments(out, submittersName, feedbackSubAttachmentFolder,
                                        s.getFeedbackAttachments());
                                out.closeEntry();
                            }
                        } // if

                        if (isAdditionalNotesEnabled && candidateDetailProvider != null) {
                            final List<String> notes = candidateDetailProvider.getAdditionalNotes(u, st)
                                    .orElse(new ArrayList<String>());
                            if (!notes.isEmpty()) {
                                final StringBuilder noteList = new StringBuilder("<ul>");
                                for (String note : notes) {
                                    noteList.append("<li>" + StringEscapeUtils.escapeHtml4(note) + "</li>");
                                }
                                noteList.append("</ul>");
                                submittersAdditionalNotesHtml
                                        .append("<tr><td style='padding-right:10px;padding-left:10px'>"
                                                + submittersString + "</td><td style='padding-right:10px'>"
                                                + noteList + "</td></tr>");
                            }
                        }
                    } else {
                        log.warn(
                                "Can't add submission: {} to zip, missing the submittee or they are no longer allowed to submit in the site",
                                s.getId());
                    }
                } catch (Exception e) {
                    caughtException = e.toString();
                    if (log.isDebugEnabled()) {
                        caughtStackTrace = ExceptionUtils.getStackTrace(e);
                    }
                    break;
                }
            } // if the user is still in site

        } // while -- there is submission

        if (caughtException == null) {
            // continue
            if (withGradeFile) {
                final ZipEntry gradesCSVEntry = new ZipEntry(root + "grades." + sheet.getFileExtension());
                out.putNextEntry(gradesCSVEntry);
                sheet.write(out);
                out.closeEntry();
            }

            if (isAdditionalNotesEnabled) {
                final ZipEntry additionalEntry = new ZipEntry(
                        root + resourceLoader.getString("assignment.additional.notes.file.title") + ".html");
                out.putNextEntry(additionalEntry);

                String htmlString = emailUtil.htmlPreamble("additionalnotes");
                htmlString += "<h1>" + resourceLoader.getString("assignment.additional.notes.export.title")
                        + "</h1>";
                htmlString += "<div>" + resourceLoader.getString("assignment.additional.notes.export.header")
                        + "</div><br/>";
                htmlString += "<table border=\"1\"  style=\"border-collapse:collapse;\"><tr><th>"
                        + resourceLoader.getString("gen.student") + "</th><th>"
                        + resourceLoader.getString("gen.notes") + "</th>" + submittersAdditionalNotesHtml
                        + "</table>";
                htmlString += "<br/><div>"
                        + resourceLoader.getString("assignment.additional.notes.export.footer") + "</div>";
                htmlString += emailUtil.htmlEnd();
                log.debug("Additional information html: " + htmlString);

                final byte[] wes = htmlString.getBytes();
                out.write(wes);
                additionalEntry.setSize(wes.length);
                out.closeEntry();
            }
        } else {
            // log the error
            exceptionMessage.append(" Exception " + caughtException
                    + " for creating submission zip file for assignment " + "\"" + assignmentTitle + "\"\n");
            if (log.isDebugEnabled()) {
                exceptionMessage.append(caughtStackTrace);
            }
        }
    } catch (IOException e) {
        exceptionMessage.append("IOException for creating submission zip file for assignment " + "\""
                + assignmentTitle + "\" exception: " + e + "\n");
    } finally {
        // Complete the ZIP file
        if (out != null) {
            try {
                out.finish();
                out.flush();
            } catch (IOException e) {
                // tried
            }
            try {
                out.close();
            } catch (IOException e) {
                // tried
            }
        }
    }
}

From source file:org.sakaiproject.assignment.impl.BaseAssignmentService.java

protected void zipSubmissions(String assignmentReference, String assignmentTitle, String gradeTypeString,
        int typeOfSubmission, Iterator submissions, OutputStream outputStream, StringBuilder exceptionMessage,
        boolean withStudentSubmissionText, boolean withStudentSubmissionAttachment, boolean withGradeFile,
        boolean withFeedbackText, boolean withFeedbackComment, boolean withFeedbackAttachment,
        boolean withoutFolders, String gradeFileFormat, boolean includeNotSubmitted) {
    ZipOutputStream out = null;

    try {//from www.  jav  a 2 s .  c o  m
        out = new ZipOutputStream(outputStream);

        // create the folder structure - named after the assignment's title
        String root = escapeInvalidCharsEntry(Validator.escapeZipEntry(assignmentTitle)) + Entity.SEPARATOR;

        SpreadsheetExporter.Type type = SpreadsheetExporter.Type.valueOf(gradeFileFormat.toUpperCase());
        SpreadsheetExporter sheet = SpreadsheetExporter.getInstance(type, assignmentTitle, gradeTypeString);

        String submittedText = "";
        if (!submissions.hasNext()) {
            exceptionMessage.append("There is no submission yet. ");
        }

        sheet.addHeader(rb.getString("grades.id"), rb.getString("grades.eid"), rb.getString("grades.lastname"),
                rb.getString("grades.firstname"), rb.getString("grades.grade"),
                rb.getString("grades.submissionTime"), rb.getString("grades.late"));

        // allow add assignment members
        List allowAddSubmissionUsers = allowAddSubmissionUsers(assignmentReference);

        // Create the ZIP file
        String submittersName = "";
        String caughtException = null;
        String caughtStackTrace = null;
        while (submissions.hasNext()) {
            AssignmentSubmission s = (AssignmentSubmission) submissions.next();
            boolean isAnon = assignmentUsesAnonymousGrading(s);
            //SAK-29314 added a new value where it's by default submitted but is marked when the user submits
            if ((s.getSubmitted() && s.isUserSubmission()) || includeNotSubmitted) {
                // get the submission user id and see if the user is still in site
                String userId = s.getSubmitterId();
                try {
                    User u = UserDirectoryService.getUser(userId);
                    if (allowAddSubmissionUsers.contains(u)) {
                        submittersName = root;

                        User[] submitters = s.getSubmitters();
                        String submittersString = "";
                        for (int i = 0; i < submitters.length; i++) {
                            if (i > 0) {
                                submittersString = submittersString.concat("; ");
                            }
                            String fullName = submitters[i].getSortName();
                            // in case the user doesn't have first name or last name
                            if (fullName.indexOf(",") == -1) {
                                fullName = fullName.concat(",");
                            }
                            submittersString = submittersString.concat(fullName);
                            // add the eid to the end of it to guarantee folder name uniqness
                            // if user Eid contains non ascii characters, the user internal id will be used
                            String userEid = submitters[i].getEid();
                            String candidateEid = escapeInvalidCharsEntry(userEid);
                            if (candidateEid.equals(userEid)) {
                                submittersString = submittersString + "(" + candidateEid + ")";
                            } else {
                                submittersString = submittersString + "(" + submitters[i].getId() + ")";
                            }
                            submittersString = escapeInvalidCharsEntry(submittersString);
                            // Work out if submission is late.
                            String latenessStatus = whenSubmissionMade(s);

                            String fullAnonId = s.getAnonymousSubmissionId();
                            String anonTitle = rb.getString("grading.anonymous.title");

                            // SAK-17606
                            if (!isAnon) {
                                sheet.addRow(submitters[i].getDisplayId(), submitters[i].getEid(),
                                        submitters[i].getLastName(), submitters[i].getFirstName(),
                                        s.getGradeDisplay(), s.getTimeSubmittedString(), latenessStatus);
                            } else {
                                sheet.addRow(fullAnonId, fullAnonId, anonTitle, anonTitle, s.getGradeDisplay(),
                                        s.getTimeSubmittedString(), latenessStatus);
                            }
                        }

                        if (StringUtils.trimToNull(submittersString) != null) {
                            submittersName = submittersName.concat(StringUtils.trimToNull(submittersString));
                            submittedText = s.getSubmittedText();

                            // SAK-17606
                            if (isAnon) {
                                submittersName = root + s.getAnonymousSubmissionId();
                                submittersString = s.getAnonymousSubmissionId();
                            }

                            if (!withoutFolders) {
                                submittersName = submittersName.concat("/");
                            } else {
                                submittersName = submittersName.concat("_");
                            }

                            // record submission timestamp
                            if (!withoutFolders) {
                                if (s.getSubmitted() && s.getTimeSubmitted() != null) {
                                    ZipEntry textEntry = new ZipEntry(submittersName + "timestamp.txt");
                                    out.putNextEntry(textEntry);
                                    byte[] b = (s.getTimeSubmitted().toString()).getBytes();
                                    out.write(b);
                                    textEntry.setSize(b.length);
                                    out.closeEntry();
                                }
                            }

                            // create the folder structure - named after the submitter's name
                            if (typeOfSubmission != Assignment.ATTACHMENT_ONLY_ASSIGNMENT_SUBMISSION
                                    && typeOfSubmission != Assignment.NON_ELECTRONIC_ASSIGNMENT_SUBMISSION) {
                                // include student submission text
                                if (withStudentSubmissionText) {
                                    // create the text file only when a text submission is allowed
                                    String submittersNameString = submittersName + submittersString;

                                    //remove folder name if Download All is without user folders
                                    if (withoutFolders) {
                                        submittersNameString = submittersName;
                                    }
                                    ZipEntry textEntry = new ZipEntry(submittersNameString + "_submissionText"
                                            + ZIP_SUBMITTED_TEXT_FILE_TYPE);
                                    out.putNextEntry(textEntry);
                                    byte[] text = submittedText.getBytes();
                                    out.write(text);
                                    textEntry.setSize(text.length);
                                    out.closeEntry();
                                }

                                // include student submission feedback text
                                if (withFeedbackText) {
                                    // create a feedbackText file into zip
                                    ZipEntry fTextEntry = new ZipEntry(submittersName + "feedbackText.html");
                                    out.putNextEntry(fTextEntry);
                                    byte[] fText = s.getFeedbackText().getBytes();
                                    out.write(fText);
                                    fTextEntry.setSize(fText.length);
                                    out.closeEntry();
                                }
                            }

                            if (typeOfSubmission != Assignment.TEXT_ONLY_ASSIGNMENT_SUBMISSION
                                    && typeOfSubmission != Assignment.NON_ELECTRONIC_ASSIGNMENT_SUBMISSION) {
                                // include student submission attachment
                                if (withStudentSubmissionAttachment) {
                                    //remove "/" that creates a folder if Download All is without user folders
                                    String sSubAttachmentFolder = submittersName
                                            + rb.getString("stuviewsubm.submissatt");//jh + "/";
                                    if (!withoutFolders) {
                                        // create a attachment folder for the submission attachments
                                        sSubAttachmentFolder = submittersName
                                                + rb.getString("stuviewsubm.submissatt") + "/";
                                        sSubAttachmentFolder = escapeInvalidCharsEntry(sSubAttachmentFolder);
                                        ZipEntry sSubAttachmentFolderEntry = new ZipEntry(sSubAttachmentFolder);
                                        out.putNextEntry(sSubAttachmentFolderEntry);

                                    } else {
                                        sSubAttachmentFolder = sSubAttachmentFolder + "_";
                                        //submittersName = submittersName.concat("_");
                                    }

                                    // add all submission attachment into the submission attachment folder
                                    zipAttachments(out, submittersName, sSubAttachmentFolder,
                                            s.getSubmittedAttachments());
                                    out.closeEntry();
                                }
                            }

                            if (withFeedbackComment) {
                                // the comments.txt file to show instructor's comments
                                ZipEntry textEntry = new ZipEntry(
                                        submittersName + "comments" + ZIP_COMMENT_FILE_TYPE);
                                out.putNextEntry(textEntry);
                                byte[] b = FormattedText.encodeUnicode(s.getFeedbackComment()).getBytes();
                                out.write(b);
                                textEntry.setSize(b.length);
                                out.closeEntry();
                            }

                            if (withFeedbackAttachment) {
                                // create an attachment folder for the feedback attachments
                                String feedbackSubAttachmentFolder = submittersName
                                        + rb.getString("download.feedback.attachment");
                                if (!withoutFolders) {
                                    feedbackSubAttachmentFolder = feedbackSubAttachmentFolder + "/";
                                    ZipEntry feedbackSubAttachmentFolderEntry = new ZipEntry(
                                            feedbackSubAttachmentFolder);
                                    out.putNextEntry(feedbackSubAttachmentFolderEntry);
                                } else {
                                    submittersName = submittersName.concat("_");
                                }

                                // add all feedback attachment folder
                                zipAttachments(out, submittersName, feedbackSubAttachmentFolder,
                                        s.getFeedbackAttachments());
                                out.closeEntry();
                            }
                        } // if
                    }
                } catch (Exception e) {
                    caughtException = e.toString();
                    if (M_log.isDebugEnabled()) {
                        caughtStackTrace = ExceptionUtils.getStackTrace(e);
                    }
                    break;
                }
            } // if the user is still in site

        } // while -- there is submission

        if (caughtException == null) {
            // continue
            if (withGradeFile) {
                ZipEntry gradesCSVEntry = new ZipEntry(root + "grades." + sheet.getFileExtension());
                out.putNextEntry(gradesCSVEntry);
                sheet.write(out);
                out.closeEntry();
            }
        } else {
            // log the error
            exceptionMessage.append(" Exception " + caughtException
                    + " for creating submission zip file for assignment " + "\"" + assignmentTitle + "\"\n");
            if (M_log.isDebugEnabled()) {
                exceptionMessage.append(caughtStackTrace);
            }
        }
    } catch (IOException e) {
        exceptionMessage.append("IOException for creating submission zip file for assignment " + "\""
                + assignmentTitle + "\" exception: " + e + "\n");
    } finally {
        // Complete the ZIP file
        if (out != null) {
            try {
                out.finish();
                out.flush();
            } catch (IOException e) {
                // tried
            }
            try {
                out.close();
            } catch (IOException e) {
                // tried
            }
        }
    }
}