001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/archive/tags/sakai_2-4-1/import-parsers/sakai-archive/src/java/org/sakaiproject/importer/impl/SakaiArchiveDataSource.java $
003: * $Id: SakaiArchiveDataSource.java 17726 2006-11-01 15:39:28Z lance@indiana.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2006 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.importer.impl;
021:
022: import java.io.ByteArrayInputStream;
023: import java.io.File;
024: import java.io.FileNotFoundException;
025: import java.io.FileOutputStream;
026: import java.io.IOException;
027: import java.util.Collection;
028: import java.util.Iterator;
029: import java.util.zip.ZipEntry;
030: import java.util.zip.ZipInputStream;
031:
032: import org.sakaiproject.archive.api.ImportMetadata;
033: import org.sakaiproject.importer.api.SakaiArchive;
034:
035: public class SakaiArchiveDataSource extends BasicImportDataSource
036: implements SakaiArchive {
037: private String sourceFolder;
038: private String localArchiveFolder;
039: private String pathToArchive;
040: private byte[] fileData;
041:
042: public SakaiArchiveDataSource(byte[] fileData,
043: String localArchiveFolder, String pathToArchive) {
044: this .fileData = fileData;
045: this .localArchiveFolder = localArchiveFolder;
046: this .pathToArchive = pathToArchive;
047: this .sourceFolder = localArchiveFolder + "/source/";
048: }
049:
050: public String getSourceFolder() {
051: return sourceFolder;
052: }
053:
054: public void setSourceFolder(String sourceFolder) {
055: this .sourceFolder = sourceFolder;
056: }
057:
058: public void buildSourceFolder(Collection selectedItems) {
059: try {
060: File dir = new File(pathToArchive + "/source"); //directory where file would be saved
061: if (!dir.exists()) {
062: dir.mkdirs();
063: }
064: for (Iterator i = selectedItems.iterator(); i.hasNext();) {
065: ImportMetadata impvalue = (ImportMetadata) i.next();
066: String selectedFileName = impvalue.getFileName();
067: ZipInputStream zipStream = new ZipInputStream(
068: new ByteArrayInputStream(fileData));
069: ZipEntry entry;
070: String entryName;
071: entry = (ZipEntry) zipStream.getNextEntry();
072: while (entry != null) {
073: entryName = entry.getName();
074: if (entryName.equals(selectedFileName)) {
075: File zipEntryFile = new File(dir.getPath()
076: + "/" + entryName);
077: if (!zipEntryFile.isDirectory()) {
078: FileOutputStream ofile = new FileOutputStream(
079: zipEntryFile);
080: byte[] buffer = new byte[1024 * 10];
081: int bytesRead;
082: while ((bytesRead = zipStream.read(buffer)) != -1) {
083: ofile.write(buffer, 0, bytesRead);
084: }
085:
086: ofile.close();
087: }
088: zipStream.closeEntry();
089: zipStream.close();
090: break;
091: }
092: entry = (ZipEntry) zipStream.getNextEntry();
093: }
094: }
095: // now take care of attachment files
096: ZipInputStream zipStream = new ZipInputStream(
097: new ByteArrayInputStream(fileData));
098: ZipEntry entry;
099: String entryName;
100: entry = (ZipEntry) zipStream.getNextEntry();
101: while (entry != null) {
102: entryName = entry.getName();
103: if (!entryName.endsWith(".xml")) {
104: File zipEntryFile = new File(dir.getPath() + "/"
105: + entryName);
106: if (!zipEntryFile.isDirectory()) {
107: FileOutputStream ofile = new FileOutputStream(
108: zipEntryFile);
109: byte[] buffer = new byte[1024 * 10];
110: int bytesRead;
111: while ((bytesRead = zipStream.read(buffer)) != -1) {
112: ofile.write(buffer, 0, bytesRead);
113: }
114:
115: ofile.close();
116: }
117: zipStream.closeEntry();
118: }
119: entry = (ZipEntry) zipStream.getNextEntry();
120: }
121: zipStream.close();
122: } catch (FileNotFoundException e) {
123: // TODO Auto-generated catch block
124: e.printStackTrace();
125: } catch (IOException e) {
126: // TODO Auto-generated catch block
127: e.printStackTrace();
128: }
129: }
130:
131: }
|