view gnv-artifacts/src/main/java/de/intevation/gnv/utils/FileUtils.java @ 1118:c01c220312d0

Made it compile again with an updated ArtifactDatabase. gnv-artifacts/trunk@4137 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 12 Mar 2012 08:31:46 +0000
parents f953c9a559d8
children
line wrap: on
line source
/*
 * Copyright (c) 2010 by Intevation GmbH
 *
 * This program is free software under the LGPL (>=v2.1)
 * Read the file LGPL.txt coming with the software for details
 * or visit http://www.gnu.org/licenses/ if it does not exist.
 */

package de.intevation.gnv.utils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.util.Stack;

import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * A helper class to provide some methods for working with files and
 * directories.
 *
 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public final class FileUtils
{
    private FileUtils() {
    }

    /**
     * Deletes everything in a directory.
     *
     * @param dir The directory.
     */
    public final static void deleteContent(File dir) {
        if (dir == null || !dir.isDirectory()) {
            return;
        }

        File[] files = dir.listFiles();
        if (files != null) {
            for (File file: files) {
                deleteRecursive(file);
            }
        }

        return;
    }

    /**
     * Delete <i>file</i> and everything in <i>file</i> if it is a directory.
     *
     * @param file The file or directory.
     * @return true, if deletion was successful - otherwise false.
     */
    public final static boolean deleteRecursive(File file) {

        if (file == null) {
            return false;
        }

        if (file.isDirectory()) {
            File [] files = file.listFiles();
            if (files != null) {
                for (File sub: files) {
                    if (!deleteRecursive(sub)) {
                        return false;
                    }
                }
            }
        }

        return file.delete();
    }

    /**
     * Put the given file or directory into a zip archive.
     *
     * @param file The file or directory.
     * @param outputStream The stream to write the archive to.
     * @throws IOException if an error occured while zip creation or writing to
     * output stream.
     */
    public static void createZipArchive(
        File         file,
        OutputStream outputStream
    )
    throws IOException
    {
        ZipOutputStream out = new ZipOutputStream(outputStream);

        if (file.isFile()) {
            copyFileToZip("", file, out);
        }
        else if (file.isDirectory()) {

            Stack<PrefixDir> stack = new Stack<PrefixDir>();
            stack.push(new PrefixDir(file.getName() + "/", file));

            while (!stack.isEmpty()) {
                PrefixDir pd = stack.pop();

                ZipEntry dirEntry = new ZipEntry(pd.prefix);
                out.putNextEntry(dirEntry);
                out.closeEntry();

                File [] files = pd.dir.listFiles();
                if (files != null) {
                    for (File sub: files) {
                        if (sub.isDirectory()) {
                            stack.push(new PrefixDir(
                                pd.prefix + sub.getName() + "/",
                                sub));
                        }
                        else if (sub.isFile()) {
                            copyFileToZip(pd.prefix, sub, out);
                        }
                    }
                }
            }
        }

        out.finish();
    }

    /**
     * A class representing a directory with a prefix.
     */
    private static final class PrefixDir {

        String prefix;
        File   dir;

        public PrefixDir(String prefix, File dir) {
            this.prefix = prefix;
            this.dir    = dir;
        }

    } // class PrefixDir

    /**
     * Write a file to zip archive.
     *
     * @param prefix A prefix.
     * @param file The file.
     * @param out The output stream.
     * @throws IOException if an error occured while writing to zip output
     * stream.
     */
    private static void copyFileToZip(
        String          prefix,
        File            file,
        ZipOutputStream out
    )
    throws IOException
    {
        String   entryName = prefix + file.getName();
        ZipEntry entry     = new ZipEntry(entryName);
        out.putNextEntry(entry);
        InputStream in = null;
        try {
            in =
                new BufferedInputStream(
                new FileInputStream(file), 20*1024);

            byte [] buf = new byte[2048];

            int r;
            while ((r = in.read(buf)) > 0) {
                out.write(buf, 0, r);
            }
        }
        finally {
            if (in != null) {
                try { in.close(); }
                catch (IOException ioe) {}
            }
        }
        out.closeEntry();
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org