001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/sam/trunk/component/src/java/org/sakaiproject/tool/assessment/services/qti/QTIService.java $
003: * $Id: QTIService.java 9273 2006-05-10 22:34:28Z daisyf@stanford.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2007 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the"License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.tool.assessment.contentpackaging;
021:
022: import java.io.ByteArrayInputStream;
023: import java.io.File;
024: import java.io.FileInputStream;
025: import java.io.FileNotFoundException;
026: import java.io.FileOutputStream;
027: import java.io.IOException;
028: import java.util.Set;
029: import java.util.TreeSet;
030: import java.util.zip.ZipEntry;
031: import java.util.zip.ZipInputStream;
032:
033: import javax.faces.context.ExternalContext;
034: import javax.faces.context.FacesContext;
035: import javax.servlet.ServletContext;
036:
037: import org.apache.commons.logging.Log;
038: import org.apache.commons.logging.LogFactory;
039: import org.sakaiproject.tool.assessment.facade.AgentFacade;
040:
041: /**
042: * <p>Copyright: Copyright (c) 2007 Sakai</p>
043: * @version $Id: QTIService.java 9273 2006-05-10 22:34:28Z daisyf@stanford.edu $
044: */
045:
046: public class ExportService {
047: private static Log log = LogFactory.getLog(ExportService.class);
048: private String qtiFilename;
049:
050: public String zipImportFile(String filename) {
051: FileInputStream fileInputStream = null;
052: FileOutputStream ofile = null;
053: ZipInputStream zipStream = null;
054: ZipEntry entry = null;
055:
056: ExternalContext external = FacesContext.getCurrentInstance()
057: .getExternalContext();
058: StringBuffer unzipLocation = new StringBuffer(
059: (String) ((ServletContext) external.getContext())
060: .getAttribute("FILEUPLOAD_REPOSITORY_PATH"));
061: log.debug("****" + unzipLocation);
062: unzipLocation.append("/jsf/upload_tmp/qti_imports/");
063: unzipLocation.append(AgentFacade.getAgentString());
064: unzipLocation.append("/unzip_files/");
065: unzipLocation.append(Long.toString(new java.util.Date()
066: .getTime()));
067:
068: try {
069: fileInputStream = new FileInputStream(new File(filename));
070: byte[] data = new byte[fileInputStream.available()];
071: fileInputStream.read(data, 0, fileInputStream.available());
072:
073: File dir = new File(unzipLocation.toString()); // directory where file would be saved
074: if (!dir.exists()) {
075: dir.mkdirs();
076: }
077:
078: Set dirsMade = new TreeSet();
079: zipStream = new ZipInputStream(new ByteArrayInputStream(
080: data));
081: entry = (ZipEntry) zipStream.getNextEntry();
082: while (entry != null) {
083: String zipName = entry.getName();
084: int ix = zipName.lastIndexOf('/');
085: if (ix > 0) {
086: String dirName = zipName.substring(0, ix);
087: if (!dirsMade.contains(dirName)) {
088: File d = new File(dir.getPath() + "/" + dirName);
089: // If it already exists as a dir, don't do anything
090: if (!(d.exists() && d.isDirectory())) {
091: // Try to create the directory, warn if it fails
092: if (!d.mkdirs()) {
093: log
094: .error("unable to mkdir "
095: + dir.getPath() + "/"
096: + dirName);
097: }
098: dirsMade.add(dirName);
099: }
100: }
101: } else {
102: if (!zipName.equals("imsmanifest.xml")) {
103: qtiFilename = zipName;
104: }
105: }
106: File zipEntryFile = new File(dir.getPath() + "/"
107: + entry.getName());
108: if (!zipEntryFile.isDirectory()) {
109: ofile = new FileOutputStream(zipEntryFile);
110: byte[] buffer = new byte[1024 * 10];
111: int bytesRead;
112: while ((bytesRead = zipStream.read(buffer)) != -1) {
113: ofile.write(buffer, 0, bytesRead);
114: }
115: }
116: zipStream.closeEntry();
117: entry = zipStream.getNextEntry();
118: }
119: } catch (FileNotFoundException e) {
120: log.error(e.getMessage());
121: } catch (IOException e) {
122: log.error(e.getMessage());
123: e.printStackTrace();
124: } finally {
125: if (ofile != null) {
126: try {
127: ofile.close();
128: } catch (IOException e) {
129: log.error(e.getMessage());
130: }
131: }
132: if (fileInputStream != null) {
133: try {
134: fileInputStream.close();
135: } catch (IOException e) {
136: log.error(e.getMessage());
137: }
138: }
139: if (zipStream != null) {
140: try {
141: zipStream.close();
142: } catch (IOException e) {
143: log.error(e.getMessage());
144: }
145: }
146: }
147: return unzipLocation.toString();
148: }
149:
150: public String getQTIFilename() {
151: return qtiFilename;
152: }
153: }
|