[7315] | 1 | package org.greenstone.gatherer.feedback;
|
---|
| 2 |
|
---|
| 3 | import java.io.*;
|
---|
| 4 | import java.util.jar.*;
|
---|
| 5 | import java.util.zip.*;
|
---|
| 6 |
|
---|
| 7 | /**
|
---|
| 8 | * This class will help to zip and unzip files.
|
---|
| 9 | * @author Veronica Liesaputra
|
---|
| 10 | */
|
---|
| 11 | public class ZipFile
|
---|
| 12 | {
|
---|
| 13 | /**
|
---|
| 14 | * This is the specified buffer size.
|
---|
| 15 | */
|
---|
| 16 | private static final int BUFFER = 2048;
|
---|
| 17 |
|
---|
| 18 | public ZipFile ()
|
---|
| 19 | {}
|
---|
| 20 |
|
---|
| 21 | /**
|
---|
| 22 | * This method will unzip the MyZipFile.zip file and delete the file.
|
---|
| 23 | */
|
---|
| 24 | public void unZipFile ()
|
---|
| 25 | {
|
---|
| 26 | try
|
---|
| 27 | {
|
---|
| 28 | BufferedOutputStream dest;
|
---|
| 29 | dest = null;
|
---|
| 30 | FileInputStream fis;
|
---|
| 31 | fis = new FileInputStream("MyZipFile.zip");
|
---|
| 32 | ZipInputStream zis;
|
---|
| 33 | zis = new ZipInputStream(new BufferedInputStream(fis));
|
---|
| 34 | ZipEntry entry;
|
---|
| 35 | while((entry = zis.getNextEntry()) != null)
|
---|
| 36 | {
|
---|
| 37 | int count;
|
---|
| 38 | byte data[];
|
---|
| 39 | data = new byte[BUFFER];
|
---|
| 40 | // write the files to the disk
|
---|
| 41 | File f;
|
---|
| 42 | f = new File(entry.getName());
|
---|
| 43 | if (isRightLogExtension(f) == true)
|
---|
| 44 | {
|
---|
| 45 | FileOutputStream fos;
|
---|
| 46 | fos = new FileOutputStream(entry.getName());
|
---|
| 47 | dest = new BufferedOutputStream(fos, BUFFER);
|
---|
| 48 | while ((count = zis.read(data, 0, BUFFER)) != -1)
|
---|
| 49 | {
|
---|
| 50 | dest.write(data, 0, count);
|
---|
| 51 | }
|
---|
| 52 | dest.flush();
|
---|
| 53 | dest.close();
|
---|
| 54 | }
|
---|
| 55 | }
|
---|
| 56 | zis.close();
|
---|
| 57 | File f3;
|
---|
| 58 | f3 = new File("MyZipFile.zip");
|
---|
| 59 | f3.delete();
|
---|
| 60 | }
|
---|
| 61 | catch (FileNotFoundException fexp) {}
|
---|
| 62 | catch(Exception e)
|
---|
| 63 | {
|
---|
| 64 | e.printStackTrace();
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | /**
|
---|
| 69 | * This method will zip all the xml files into one Jar file.
|
---|
| 70 | */
|
---|
| 71 | public void sendZipXMLFile ()
|
---|
| 72 | {
|
---|
| 73 | try
|
---|
| 74 | {
|
---|
| 75 | String dirname;
|
---|
| 76 | dirname = "xmlfeedback/";
|
---|
| 77 | BufferedInputStream origin;
|
---|
| 78 | origin = null;
|
---|
| 79 | FileOutputStream dest;
|
---|
| 80 | dest = new FileOutputStream(dirname + "Jar" +
|
---|
| 81 | FeedbackInterface.getCode() +
|
---|
| 82 | "File.jar");
|
---|
| 83 | JarOutputStream out;
|
---|
| 84 | out = new JarOutputStream(new BufferedOutputStream(dest));
|
---|
| 85 | out.setMethod(ZipOutputStream.DEFLATED);
|
---|
| 86 | byte data[];
|
---|
| 87 | data = new byte[BUFFER];
|
---|
| 88 |
|
---|
| 89 | File f;
|
---|
| 90 | f = new File(dirname + ".");
|
---|
| 91 | File[] fileList;
|
---|
| 92 | fileList = f.listFiles();
|
---|
| 93 | File[] files;
|
---|
| 94 | files = new File[fileList.length + 8];
|
---|
| 95 |
|
---|
| 96 | int index,j,i;
|
---|
| 97 | index = 0;
|
---|
| 98 | for (j = 0 ; j < fileList.length ; j++)
|
---|
| 99 | {
|
---|
| 100 | String name;
|
---|
| 101 | name = fileList[j].getName();
|
---|
| 102 |
|
---|
| 103 | if (isRightXMLExtension(fileList[j]) == true)
|
---|
| 104 | {
|
---|
| 105 | files[index] = fileList[j];
|
---|
| 106 | index++;
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | for ( i = 0; i < index; i++)
|
---|
| 111 | {
|
---|
| 112 | FileInputStream fi;
|
---|
| 113 | fi = new FileInputStream(files[i]);
|
---|
| 114 | origin = new BufferedInputStream(fi, BUFFER);
|
---|
| 115 | ZipEntry entry;
|
---|
| 116 | entry = new ZipEntry(files[i].getName());
|
---|
| 117 | out.putNextEntry(entry);
|
---|
| 118 | int count;
|
---|
| 119 | while((count = origin.read(data, 0,BUFFER)) != -1)
|
---|
| 120 | {
|
---|
| 121 | out.write(data, 0, count);
|
---|
| 122 | }
|
---|
| 123 | origin.close();
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | out.close();
|
---|
| 127 | }
|
---|
| 128 | catch(Exception e)
|
---|
| 129 | {
|
---|
| 130 | e.printStackTrace();
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | /**
|
---|
| 135 | * This method will zip all .log file to MyZipFile.zip file and it will
|
---|
| 136 | * delete the history.log file.
|
---|
| 137 | */
|
---|
| 138 | public void sendZipFile ()
|
---|
| 139 | {
|
---|
| 140 | try
|
---|
| 141 | {
|
---|
| 142 | BufferedInputStream origin;
|
---|
| 143 | origin = null;
|
---|
| 144 | FileOutputStream dest;
|
---|
| 145 | dest = new FileOutputStream("MyZipFile.zip");
|
---|
| 146 | ZipOutputStream out;
|
---|
| 147 | out = new ZipOutputStream(new BufferedOutputStream(dest));
|
---|
| 148 | out.setMethod(ZipOutputStream.DEFLATED);
|
---|
| 149 | byte data[];
|
---|
| 150 | data = new byte[BUFFER];
|
---|
| 151 |
|
---|
| 152 | File f;
|
---|
| 153 | f = new File(".");
|
---|
| 154 | File[] fileList;
|
---|
| 155 | fileList = f.listFiles();
|
---|
| 156 | File[] files;
|
---|
| 157 | files = new File[fileList.length];
|
---|
| 158 |
|
---|
| 159 | int index,i,j;
|
---|
| 160 | index = 0;
|
---|
| 161 | for ( j = 0 ; j < fileList.length ; j++)
|
---|
| 162 | {
|
---|
| 163 | if (isRightLogExtension(fileList[j]) == true)
|
---|
| 164 | {
|
---|
| 165 | files[index] = fileList[j];
|
---|
| 166 | index++;
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | for (i = 0; i < index; i++)
|
---|
| 171 | {
|
---|
| 172 | FileInputStream fi;
|
---|
| 173 | fi = new FileInputStream(files[i]);
|
---|
| 174 | origin = new BufferedInputStream(fi, BUFFER);
|
---|
| 175 | ZipEntry entry;
|
---|
| 176 | entry = new ZipEntry(files[i].getName());
|
---|
| 177 | out.putNextEntry(entry);
|
---|
| 178 | int count;
|
---|
| 179 | while((count = origin.read(data, 0,BUFFER)) != -1)
|
---|
| 180 | {
|
---|
| 181 | out.write(data, 0, count);
|
---|
| 182 | }
|
---|
| 183 | origin.close();
|
---|
| 184 | }
|
---|
| 185 | out.close();
|
---|
| 186 | File f2;
|
---|
| 187 | f2 = new File("history.log");
|
---|
| 188 | f2.delete();
|
---|
| 189 | }
|
---|
| 190 | catch(Exception e)
|
---|
| 191 | {
|
---|
| 192 | e.printStackTrace();
|
---|
| 193 | }
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | /**
|
---|
| 197 | * This method will get the extension of the given file.
|
---|
| 198 | * @param f the file we want to know the extension.
|
---|
| 199 | * @return the file extension.
|
---|
| 200 | */
|
---|
| 201 | public String getExtension(File f)
|
---|
| 202 | {
|
---|
| 203 | String ext;
|
---|
| 204 | ext = null;
|
---|
| 205 | String s;
|
---|
| 206 | s = f.getName();
|
---|
| 207 | int i;
|
---|
| 208 | i = s.lastIndexOf('.');
|
---|
| 209 |
|
---|
| 210 | if (i > 0 && i < s.length() - 1)
|
---|
| 211 | {
|
---|
| 212 | ext = s.substring(i+1).toLowerCase();
|
---|
| 213 | }
|
---|
| 214 | return ext;
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | /**
|
---|
| 218 | * This method will check whether or not the give file has
|
---|
| 219 | * an .xml extension.
|
---|
| 220 | * @param f the file we want to check the extension.
|
---|
| 221 | * @return whether or not the file has xml as its file extension.
|
---|
| 222 | */
|
---|
| 223 | public boolean isRightXMLExtension(File f)
|
---|
| 224 | {
|
---|
| 225 | if (f.isDirectory() == true)
|
---|
| 226 | {
|
---|
| 227 | return false;
|
---|
| 228 | }
|
---|
| 229 | String ext;
|
---|
| 230 | ext = getExtension(f);
|
---|
| 231 | if ((ext != null) && (ext.compareTo("xml") == 0))
|
---|
| 232 | return true;
|
---|
| 233 | else
|
---|
| 234 | return false;
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | /**
|
---|
| 238 | * This method will check whether or not the give file has
|
---|
| 239 | * an .log extension.
|
---|
| 240 | * @param f the file we want to check the extension.
|
---|
| 241 | * @return whether or not the file has log as its file extension.
|
---|
| 242 | */
|
---|
| 243 | public boolean isRightLogExtension(File f)
|
---|
| 244 | {
|
---|
| 245 | if (f.isDirectory() == true)
|
---|
| 246 | return false;
|
---|
| 247 |
|
---|
| 248 | String ext;
|
---|
| 249 | ext = getExtension(f);
|
---|
| 250 |
|
---|
| 251 | if ((ext != null) && (ext.compareTo("log") == 0))
|
---|
| 252 | return true;
|
---|
| 253 | else
|
---|
| 254 | return false;
|
---|
| 255 | }
|
---|
| 256 | }
|
---|
| 257 |
|
---|
| 258 |
|
---|
| 259 |
|
---|
| 260 |
|
---|
| 261 |
|
---|
| 262 |
|
---|
| 263 |
|
---|
| 264 |
|
---|
| 265 |
|
---|
| 266 |
|
---|