/* (c) 2001 http://www.orgdot.com: you can copy, use, modify and distribute this
code and/or artwork for educational, commercial or recreational use. all we ask is
that you include this copyright notice in the materialyou distribute. for compiled
code, you will need to make accessible this copyright notice somewhere in the
distribution, and/or via a link on the web. there are several reasons for this
caveat - the most important being that open source is based on one main principle:
what you find and use, others should also have access to. don't keep it to yourself!
this software is provided by the author and contributors "as is" and any express or
implied warranties, including, but not limited to, the implied warranties of
merchantability and fitness for a particular purpose are disclaimed. in no event
shall the author or contributors be liable for any direct, indirect, incidental,
special, exemplary, or consequential damages (including, but not limited to,
procurement of substitute goods or services; loss of use, data, or profits; or
business interruption) however caused and on any theory of liability, whether in
contract, strict liability, or tort (including negligence or otherwise) arising in
any way out of the use of this software, even if advised of the possibility of
such damage. */
/* ************************************* **
KEEPS TRACK OF LINKS IN A FLASH BANNER
** ************************************* */
/*
web.xml snippet:
zipper
com.orgdot.servlet.ZipAll
zip_out_file_name
download.zip
zip_root_dir
c:/apache/www.orgdot.com/
download_path
http://www.orgdot.com/
dont_zip_endings_0
.fla
dont_zip_endings_1
.mov
dont_zip_endings_2
.zip
dont_zip_files_0
uploads
dont_zip_files_1
WEB-INF
*/
package com.orgdot.servlet;
import java.util.zip.*;
import java.util.Vector;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ZipAll extends HttpServlet
{
public final static String
DOWNLOAD_PATH = "download_path",
ZIP_OUT_FILE = "zip_out_file_name",
ZIP_ROOT_DIR = "zip_root_dir",
DONT_ZIP_FILES = "dont_zip_files_",
DONT_ZIP_ENDINGS = "dont_zip_endings_";
public void service
(
HttpServletRequest request,
HttpServletResponse response
)
throws IOException, ServletException
{
ZipOutputStream zos = new ZipOutputStream
(
new FileOutputStream
(
new File
(
getServletConfig().getInitParameter (ZIP_ROOT_DIR),
getServletConfig().getInitParameter (ZIP_OUT_FILE)
)
)
);
String download_file =
getServletConfig().getInitParameter (DOWNLOAD_PATH) +
getServletConfig().getInitParameter (ZIP_OUT_FILE);
StringBuffer str_b = zipDir
(
zos,
new File ( getServletConfig().getInitParameter (ZIP_ROOT_DIR) ),
new byte[1024],
new String(),
configStringArray ( getServletConfig(), DONT_ZIP_FILES),
configStringArray ( getServletConfig(), DONT_ZIP_ENDINGS),
new StringBuffer().
append ("\n
\n").
append ("klick below to download all updated files in one single, zipped archive!
\n").
append ("").
append (download_file).
append ("
list of zipped files:\n\n");
// clean up the zip file
zos.close();
//this request should be done each time a user accesses the data
response.setHeader ("Cache-Control", "no-cache");
response.setHeader ("Pragma", "no-cache");
response.setContentType ("text/html");
OutputStream os = response.getOutputStream();
os.write ( str_b.toString().getBytes() );
os.close();
}
private final static StringBuffer zipDir
(
ZipOutputStream zos,
File dir,
byte buf [],
String web_view_path,
String hide_files[],
String hide_endings[],
StringBuffer str_b
)
throws IOException
{
if ( stringArrayIndexOf ( hide_files, dir.getName() ) > -1 )
return str_b;
if (web_view_path.equals ("")) web_view_path = "/";
else web_view_path += dir.getName() + "/";
String file_names [] = dir.list();
str_b.
append ("directory: ").
append (web_view_path).
append ("\n");
int read_len;
for (int i = 0; i < file_names.length; i++)
{
File f = new File (dir, file_names[i]);
if (f.isDirectory()) str_b = zipDir
(
zos,
f,
buf,
web_view_path,
hide_files,
hide_endings,
str_b
);
else if ( ! stringArrayEndsWith ( hide_endings, f.getName() ) )
{
ZipEntry z_file_entry = new ZipEntry (web_view_path.substring (1) + f.getName());
zos.putNextEntry (z_file_entry);
FileInputStream fis = new FileInputStream(f);
while ((read_len = fis.read(buf)) >= 0) zos.write(buf, 0, read_len);
fis.close();
str_b.
append ("- file: ").
append (f.getName()).
append (" size: ").
append (f.length()).
append ("
\n");
}
}
return str_b.append ("
\n");
}
private final static String configStringArray
(
ServletConfig config_,
String arr_name
)[]
{
Vector vect = new Vector();
String str;
int i = 0;
while ( (str = config_.getInitParameter (arr_name + i++)) != null) vect.addElement (str);
String strings [] = new String [vect.size()];
vect.copyInto (strings);
return strings;
}
private final static boolean stringArrayEndsWith
(
String strings[],
String val
)
{
if (val != null && strings != null)
for ( int i = 0, len = strings.length; i < len; i++)
if (val.endsWith (strings[i])) return true;
return false;
}
private final static int stringArrayIndexOf
(
String strings[],
String val
)
{
if (val != null && strings != null)
for ( int i = 0, len = strings.length; i < len; i++)
if (val.equals (strings[i])) return i;
return - 1;
}
}