You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by gn...@apache.org on 2008/10/13 14:36:54 UTC

svn commit: r704084 - /servicemix/smx4/kernel/trunk/filemonitor/src/main/java/org/apache/servicemix/kernel/filemonitor/JarUtil.java

Author: gnodet
Date: Mon Oct 13 05:36:53 2008
New Revision: 704084

URL: http://svn.apache.org/viewvc?rev=704084&view=rev
Log:
SMX4KNL-99: Remove ant from the kernel distribution and replace its use in the filemonitor

Added:
    servicemix/smx4/kernel/trunk/filemonitor/src/main/java/org/apache/servicemix/kernel/filemonitor/JarUtil.java

Added: servicemix/smx4/kernel/trunk/filemonitor/src/main/java/org/apache/servicemix/kernel/filemonitor/JarUtil.java
URL: http://svn.apache.org/viewvc/servicemix/smx4/kernel/trunk/filemonitor/src/main/java/org/apache/servicemix/kernel/filemonitor/JarUtil.java?rev=704084&view=auto
==============================================================================
--- servicemix/smx4/kernel/trunk/filemonitor/src/main/java/org/apache/servicemix/kernel/filemonitor/JarUtil.java (added)
+++ servicemix/smx4/kernel/trunk/filemonitor/src/main/java/org/apache/servicemix/kernel/filemonitor/JarUtil.java Mon Oct 13 05:36:53 2008
@@ -0,0 +1,122 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicemix.kernel.filemonitor;
+
+import java.io.FileOutputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.FileInputStream;
+import java.io.BufferedOutputStream;
+import java.util.zip.ZipOutputStream;
+import java.util.zip.ZipEntry;
+import java.util.jar.JarOutputStream;
+import java.util.jar.JarFile;
+import java.util.Collections;
+import java.util.Set;
+
+public class JarUtil {
+
+    /**
+     * Zip up a directory
+     *
+     * @param directory
+     * @param zipName
+     * @throws IOException
+     */
+    public static void zipDir(String directory, String zipName) throws IOException {
+        // create a ZipOutputStream to zip the data to
+        ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipName)));
+        String path = "";
+        zipDir(directory, zos, path, Collections.<String>emptySet());
+        // close the stream
+        zos.close();
+    }
+
+    /**
+     * Jar up a directory
+     *
+     * @param directory
+     * @param zipName
+     * @throws IOException
+     */
+    public static void jarDir(String directory, String zipName) throws IOException {
+        // create a ZipOutputStream to zip the data to
+        JarOutputStream zos = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(zipName)));
+        String path = "";
+        File manFile = new File(directory, JarFile.MANIFEST_NAME);
+        if (manFile.exists()) {
+            byte[] readBuffer = new byte[8192];
+            FileInputStream fis = new FileInputStream(manFile);
+            try {
+                ZipEntry anEntry = new ZipEntry(JarFile.MANIFEST_NAME);
+                zos.putNextEntry(anEntry);
+                int bytesIn = fis.read(readBuffer);
+                while (bytesIn != -1) {
+                    zos.write(readBuffer, 0, bytesIn);
+                    bytesIn = fis.read(readBuffer);
+                }
+            } finally {
+                fis.close();
+            }
+            zos.closeEntry();
+        }
+        zipDir(directory, zos, path, Collections.singleton(JarFile.MANIFEST_NAME));
+        // close the stream
+        zos.close();
+    }
+
+    /**
+     * Zip up a directory path
+     * @param directory
+     * @param zos
+     * @param path
+     * @throws IOException
+     */
+    public static void zipDir(String directory, ZipOutputStream zos, String path, Set<String> exclusions) throws IOException {
+        File zipDir = new File(directory);
+        // get a listing of the directory content
+        String[] dirList = zipDir.list();
+        byte[] readBuffer = new byte[8192];
+        int bytesIn = 0;
+        // loop through dirList, and zip the files
+        for (int i = 0; i < dirList.length; i++) {
+            File f = new File(zipDir, dirList[i]);
+            if (f.isDirectory()) {
+                String filePath = f.getPath();
+                zipDir(filePath, zos, path + f.getName() + "/", exclusions);
+                continue;
+            }
+            String entry = path + f.getName();
+            if (!exclusions.contains(entry)) {
+                FileInputStream fis = new FileInputStream(f);
+                try {
+                    ZipEntry anEntry = new ZipEntry(entry);
+                    zos.putNextEntry(anEntry);
+                    bytesIn = fis.read(readBuffer);
+                    while (bytesIn != -1) {
+                        zos.write(readBuffer, 0, bytesIn);
+                        bytesIn = fis.read(readBuffer);
+                    }
+                } finally {
+                    fis.close();
+                }
+            }
+        }
+    }
+
+}