001: /*
002: * JFolder, Copyright 2001-2006 Gary Steinmetz
003: *
004: * Distributable under LGPL license.
005: * See terms of license at gnu.org.
006: */
007:
008: package org.jfolder.common.files;
009:
010: //base classes
011: import java.io.ByteArrayOutputStream;
012: import java.io.File;
013: import java.io.FileOutputStream;
014: import java.io.IOException;
015: import java.io.InputStream;
016: import java.io.OutputStream;
017: import java.net.HttpURLConnection;
018: import java.net.URL;
019: import java.util.ArrayList;
020: import java.util.zip.ZipOutputStream;
021:
022: //project specific classes
023: import org.jfolder.common.UnexpectedSystemException;
024: import org.jfolder.common.utils.misc.MiscHelper;
025:
026: //other classes
027:
028: public class VirtualFileSystemHelper {
029:
030: private VirtualFileSystemHelper() {
031: }
032:
033: public final static byte[] putZippedFileSystem(
034: VirtualFileSystemRoot inVfsr, URL inDestination) {
035: //
036: try {
037:
038: ByteArrayOutputStream outValue = new ByteArrayOutputStream();
039:
040: HttpURLConnection huc = (HttpURLConnection) inDestination
041: .openConnection();
042: //
043: huc.setRequestMethod("PUT");
044: huc.setDoOutput(true);
045: huc.connect();
046: OutputStream os = huc.getOutputStream();
047: ZipOutputStream zos = new ZipOutputStream(os);
048: inVfsr.zip(zos);
049: zos.flush();
050: zos.close();
051: os.flush();
052: os.close();
053: //
054: InputStream is = huc.getInputStream();
055: byte buffer[] = new byte[1024];
056: int readSize = 0;
057: while ((readSize = is.read(buffer, 0, buffer.length)) != -1) {
058: outValue.write(buffer, 0, readSize);
059: }
060: is.close();
061: //
062: huc.disconnect();
063:
064: return outValue.toByteArray();
065: } catch (IOException ioe) {
066: throw new UnexpectedSystemException(ioe);
067: }
068: }
069:
070: //
071: //
072: //
073: public final static void writeFileSystem(File inCurrentDir,
074: VirtualFileSystemHolder inCurrentVfsh) {
075: //
076: ArrayList localDirs = inCurrentVfsh.getLocalDirectories();
077: for (int i = 0; i < localDirs.size(); i++) {
078: VirtualFileSystemDirectory nextVfsd = ((VirtualFileSystemDirectory) localDirs
079: .get(i));
080: File nextSubDir = new File(inCurrentDir, nextVfsd.getName());
081: nextSubDir.mkdir();
082: writeFileSystem(nextSubDir, nextVfsd);
083: }
084: //
085: ArrayList localFiles = inCurrentVfsh.getLocalFiles();
086: for (int i = 0; i < localFiles.size(); i++) {
087: VirtualFileSystemFile nextVfsf = ((VirtualFileSystemFile) localFiles
088: .get(i));
089: File nextSubFile = new File(inCurrentDir, nextVfsf
090: .getName());
091: MiscHelper.writeBinaryFile(nextSubFile, nextVfsf
092: .getContent());
093: }
094: }
095:
096: //
097: //
098: //
099: public final static void pruneEmptyNestedDirectories(
100: VirtualFileSystemHolder inCurrentVfsh) {
101: //
102: ArrayList localDirs = inCurrentVfsh.getLocalDirectories();
103: for (int i = 0; i < localDirs.size(); i++) {
104: VirtualFileSystemDirectory nextVfsd = ((VirtualFileSystemDirectory) localDirs
105: .get(i));
106: //
107: pruneEmptyNestedDirectories(nextVfsd);
108: //
109: if (nextVfsd.getLocalDirectories().size() == 0
110: && nextVfsd.getLocalFiles().size() == 0) {
111: //
112: inCurrentVfsh.removeDirectory(nextVfsd.getName());
113: }
114: //File nextSubDir = new File(inCurrentDir, nextVfsd.getName());
115: //nextSubDir.mkdir();
116: //writeFileSystem(nextSubDir, nextVfsd);
117: }
118: }
119: }
|