[Red5devs] [red5 commit] r3074 - in java/server/trunk/src/org/red5/server: service util
codesite-noreply at google.com
codesite-noreply at google.com
Sat Sep 20 19:07:38 PDT 2008
Author: daccattato
Date: Sat Sep 20 19:06:39 2008
New Revision: 3074
Modified:
java/server/trunk/src/org/red5/server/service/Installer.java
java/server/trunk/src/org/red5/server/util/FileUtil.java
Log:
Installer: worked on the error received when downloading a Zip archive. An
invalide ZipFile error was being produced.
However, receiving the stream as a byte array correctly downloads the war
file.
FileUtil: worked on the unzip method as it was incorrectly unzipping
Modified: java/server/trunk/src/org/red5/server/service/Installer.java
==============================================================================
--- java/server/trunk/src/org/red5/server/service/Installer.java (original)
+++ java/server/trunk/src/org/red5/server/service/Installer.java Sat Sep 20
19:06:39 2008
@@ -50,6 +50,7 @@
* applications from a given url.
*
* @author Paul Gregoire (mondain at gmail.com)
+ * @author Dominick Accattato (daccattato at gmail.com)
*/
public class Installer {
@@ -215,14 +216,14 @@
//create output file
fos = new FileOutputStream(srcDir + '/' + applicationWarName);
log.debug("Writing response to {}/{}", srcDir, applicationWarName);
- InputStream is = method.getResponseBodyAsStream();
- byte[] buf = new byte[512];
- while (is.read(buf) != -1) {
- fos.write(buf);
- }
- is.close();
+
+ // have to receive the response as a byte array. This has the
advantage of writing to the filesystem
+ // faster and it also works on macs ;)
+ byte[] buf = method.getResponseBody();
+ fos.write(buf);
+ fos.close();
fos.flush();
- //
+
result = true;
} catch (HttpException he) {
log.error("Http error connecting to {}", applicationRepositoryUrl,
he);
Modified: java/server/trunk/src/org/red5/server/util/FileUtil.java
==============================================================================
--- java/server/trunk/src/org/red5/server/util/FileUtil.java (original)
+++ java/server/trunk/src/org/red5/server/util/FileUtil.java Sat Sep 20
19:06:39 2008
@@ -19,18 +19,23 @@
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
+
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
+import java.util.Enumeration;
import java.util.Random;
import java.util.zip.Adler32;
import java.util.zip.CheckedInputStream;
import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
import org.slf4j.Logger;
@@ -41,6 +46,7 @@
* manipulation functions.
*
* @author Paul Gregoire (mondain at gmail.com)
+ * @author Dominick Accattato (daccattato at gmail.com)
*/
public class FileUtil {
@@ -315,61 +321,121 @@
dir = null;
return created;
}
-
+
/**
- * Unzips a given archive to a specified destination directory.
+ * Unzips a war file to an application located under the webapps directory
*
- * @param compressedFileName
+ * @param compressedFileName
+ * The String name of the war file
* @param destinationDir
+ * The destination directory, ie: webapps
*/
public static void unzip(String compressedFileName, String
destinationDir) {
- log.debug("Unzip - file: {} destination: {}", compressedFileName,
destinationDir);
+
+ //strip everything except the applications name
+ String dirName = compressedFileName.substring(0,
compressedFileName.indexOf('-'));
+ //String tmpDir = System.getProperty("java.io.tmpdir");
+ File zipDir = new File(compressedFileName);
+ File parent = zipDir.getParentFile();
+ //File tmpDir = new File(System.getProperty("java.io.tmpdir"), dirName);
+ File tmpDir = new File(destinationDir);
+
+ // make the war directory
+ System.out.println("making directory: " + tmpDir.mkdirs());
+
try {
- final int BUFFER = 2048;
- BufferedOutputStream dest = null;
- FileInputStream fis = new FileInputStream(compressedFileName);
- CheckedInputStream checksum = new CheckedInputStream(fis,
- new Adler32());
- ZipInputStream zis = new ZipInputStream(new BufferedInputStream(
- checksum));
- ZipEntry entry;
- while ((entry = zis.getNextEntry()) != null) {
- log.debug("Extracting: {}", entry);
- String name = entry.getName();
- int count;
- byte data[] = new byte[BUFFER];
- // write the files to the disk
- File destFile = new File(destinationDir, name);
- log.debug("Absolute path: {}", destFile.getAbsolutePath());
- //create dirs as needed, look for file extension to determine type
- if (entry.isDirectory()) {
- log.debug("Entry is detected as a directory");
- if (destFile.mkdirs()) {
- log.debug("Directory created: {}", destFile.getName());
- } else {
- log.warn("Directory was not created: {}", destFile.getName());
- }
- destFile = null;
+ ZipFile zf = new ZipFile(compressedFileName);
+ Enumeration e = zf.entries();
+ while(e.hasMoreElements()) {
+ ZipEntry ze = (ZipEntry) e.nextElement();
+ System.out.println("Unzipping " + ze.getName());
+ if(ze.isDirectory()) {
+ System.out.println("is a directory");
+ File dir = new File(tmpDir + "/" + ze.getName());
+ Boolean tmp = dir.mkdir();
+ System.out.println(tmp);
continue;
- }
-
- FileOutputStream fos = new FileOutputStream(destFile);
- dest = new BufferedOutputStream(fos, BUFFER);
- while ((count = zis.read(data, 0, BUFFER)) != -1) {
- dest.write(data, 0, count);
}
- dest.flush();
- dest.close();
- destFile = null;
+ FileOutputStream fout = new FileOutputStream(tmpDir + "/" +
ze.getName());
+ java.io.InputStream in = zf.getInputStream(ze);
+ copy(in, fout);
+ in.close();
+ fout.close();
}
- zis.close();
- log.debug("Checksum: {}", checksum.getChecksum().getValue());
- } catch (Exception e) {
- log.error("Error unzipping {}", compressedFileName, e);
+ } catch (IOException e) {
+ System.err.println(e);
e.printStackTrace();
}
-
}
+
+ public static void copy(InputStream in, OutputStream out) throws
IOException {
+
+ synchronized(in) {
+ synchronized(out) {
+ byte[] buffer = new byte[256];
+ while (true) {
+ int bytesRead = in.read(buffer);
+ if(bytesRead == -1) break;
+ out.write(buffer, 0, bytesRead);
+ }
+ }
+ }
+ }
+
+ /**
+ * Unzips a given archive to a specified destination directory.
+ *
+ * @param compressedFileName
+ * @param destinationDir
+ */
+// public static void unzip(String compressedFileName, String
destinationDir) {
+// log.debug("Unzip - file: {} destination: {}", compressedFileName,
destinationDir);
+// try {
+// final int BUFFER = 2048;
+// BufferedOutputStream dest = null;
+// FileInputStream fis = new FileInputStream(compressedFileName);
+// CheckedInputStream checksum = new CheckedInputStream(fis,
+// new Adler32());
+// ZipInputStream zis = new ZipInputStream(new BufferedInputStream(
+// checksum));
+// ZipEntry entry;
+// while ((entry = zis.getNextEntry()) != null) {
+// log.debug("Extracting: {}", entry);
+// String name = entry.getName();
+// int count;
+// byte data[] = new byte[BUFFER];
+// // write the files to the disk
+// File destFile = new File(destinationDir, name);
+// log.debug("Absolute path: {}", destFile.getAbsolutePath());
+// //create dirs as needed, look for file extension to determine type
+// if (entry.isDirectory()) {
+// log.debug("Entry is detected as a directory");
+// if (destFile.mkdirs()) {
+// log.debug("Directory created: {}", destFile.getName());
+// } else {
+// log.warn("Directory was not created: {}", destFile.getName());
+// }
+// destFile = null;
+// continue;
+// }
+//
+// FileOutputStream fos = new FileOutputStream(destFile);
+// dest = new BufferedOutputStream(fos, BUFFER);
+// while ((count = zis.read(data, 0, BUFFER)) != -1) {
+// dest.write(data, 0, count);
+// }
+// dest.flush();
+// dest.close();
+// destFile = null;
+// }
+// zis.close();
+// log.debug("Checksum: {}", checksum.getChecksum().getValue());
+// } catch (Exception e) {
+// log.error("Error unzipping {}", compressedFileName, e);
+// e.printStackTrace();
+// }
+//
+// }
/**
* Quick-n-dirty directory formatting to support launching in windows,
specifically from ant.
More information about the Red5devs
mailing list