corpus-services  1.0
ZipCorpus.java
Go to the documentation of this file.
1 /*
2  * To change this license header, choose License Headers in Project Properties.
3  * To change this template file, choose Tools | Templates
4  * and open the template in the editor.
5  */
6 package de.uni_hamburg.corpora.publication;
7 
12 import java.io.File;
13 import java.io.FileInputStream;
14 import java.io.FileOutputStream;
15 import java.io.IOException;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.List;
19 import java.util.zip.ZipEntry;
20 import java.util.zip.ZipOutputStream;
21 
32 public class ZipCorpus extends Publisher implements CorpusFunction {
33 
34  List<String> fileList;
35  //folder
36  String SOURCE_FOLDER = "";
37  //get path of zip file corpus/resources/corpus.zip
38  String OUTPUT_ZIP_FILE = "";
39  Boolean AUDIO = false;
40  CorpusData comadata;
41 
42  public ZipCorpus() {
43  super();
44  fileList = new ArrayList<String>();
45  }
46 
47  /*
48  public static void main(String[] args) {
49  ZipCorpus appZip = new ZipCorpus();
50  System.out.println(SOURCE_FOLDER);
51  appZip.generateFileList(new File(SOURCE_FOLDER));
52  appZip.zipIt(OUTPUT_ZIP_FILE, AUDIO);
53  }
54  */
60  public Report zipIt(CorpusData comadata, String zipFile, Boolean AUDIO) {
61  Report stats = new Report();
62  //get name of folder
63  if (zipFile.equals("")) {
64  String SOURCE_FOLDER_NAME = comadata.getFilenameWithoutFileEnding();
65  if (AUDIO) {
66  zipFile = SOURCE_FOLDER + "resources" + File.separator + SOURCE_FOLDER_NAME + "WithAudio.zip";
67  } else {
68  zipFile = SOURCE_FOLDER + "resources" + File.separator + SOURCE_FOLDER_NAME + "NoAudio.zip";
69  }
70  }
71  byte[] buffer = new byte[1024];
72 
73  try {
74 
75  FileOutputStream fos = new FileOutputStream(zipFile);
76  ZipOutputStream zos = new ZipOutputStream(fos);
77 
78  System.out.println("Output to Zip : " + zipFile);
79 
80  for (String file : this.fileList) {
81 
82  System.out.println("File Added : " + file);
83  ZipEntry ze = new ZipEntry(file);
84  zos.putNextEntry(ze);
85 
86  FileInputStream in
87  = new FileInputStream(file);
88 
89  int len;
90  while ((len = in.read(buffer)) > 0) {
91  zos.write(buffer, 0, len);
92  }
93 
94  in.close();
95  }
96 
97  zos.closeEntry();
98  //remember close it
99  zos.close();
100  System.out.println("Done");
101  stats.addCorrect(function, comadata, "Successfully created zip file at " + zipFile);
102  } catch (IOException ex) {
103  stats.addException(ex, function, comadata, "Unknown IO exception");
104  }
105  return stats;
106  }
107 
113  public Report generateFileList(File node) {
114  Report stats = new Report();
115  //add file only
116  if (node.isFile()) {
117  if (AUDIO) {
118  if (node.getName().endsWith(".exb") || node.getName().endsWith(".exs") || node.getName().endsWith(".coma") || node.getName().endsWith(".pdf") || node.getName().endsWith(".mp3")) {
119  System.out.println(node.getName());
120  fileList.add(generateZipEntry(node.getAbsoluteFile().toString()));
121  stats.addCorrect(function, comadata, node.getAbsoluteFile().toString() + " added to filelist");
122  }
123  } else if (node.getName().endsWith(".exb") || node.getName().endsWith(".exs") || node.getName().endsWith(".coma") || node.getName().endsWith(".pdf")) {
124  System.out.println(node.getName());
125  fileList.add(generateZipEntry(node.getAbsoluteFile().toString()));
126  stats.addCorrect(function, comadata, node.getAbsoluteFile().toString() + " added to filelist");
127  }
128  }
129  if (node.isDirectory()) {
130  String[] subNote = node.list();
131  for (String filename : subNote) {
132  System.out.println(node.getName());
133  generateFileList(new File(node, filename));
134  }
135  }
136  return stats;
137  }
138 
145  private String generateZipEntry(String file) {
146  //return file.substring(SOURCE_FOLDER.length() + 1, file.length());
147  return file;
148  }
149 
150  @Override
151  public Report function(CorpusData cd) {
152  Report stats = new Report();
153  comadata = cd;
154  if (SOURCE_FOLDER.equals("")){
155  SOURCE_FOLDER = cd.getParentURL().getPath();
156  }
157  stats = generateFileList(new File(SOURCE_FOLDER));
158  stats.merge(zipIt(cd, OUTPUT_ZIP_FILE, AUDIO));
159  return stats;
160  }
161 
162  @Override
163  public Report function(Corpus c) {
164  Report stats = new Report();
165  comadata = c.getComaData();
166  SOURCE_FOLDER = cd.getParentURL().getPath();
167  stats = generateFileList(new File(SOURCE_FOLDER));
168  stats.merge(zipIt(cd, OUTPUT_ZIP_FILE, AUDIO));
169  return stats;
170  }
171 
172  @Override
173  public Collection<Class<? extends CorpusData>> getIsUsableFor() {
174  try {
175  Class cl = Class.forName("de.uni_hamburg.corpora.ComaData");
176  IsUsableFor.add(cl);
177  } catch (ClassNotFoundException ex) {
178  report.addException(ex, "Usable class not found.");
179  }
180  return IsUsableFor;
181  }
182 
183  public void setSourceFolder(String s) {
184  SOURCE_FOLDER = s;
185  }
186 
187  public void setOutputFile(String s) {
188  OUTPUT_ZIP_FILE = s;
189  }
190 
191  public void setWithAudio(String s) {
192  report = new Report();
193  if (s.equalsIgnoreCase("true") || s.equalsIgnoreCase("wahr") || s.equalsIgnoreCase("ja")) {
194  AUDIO = true;
195  } else if (s.equalsIgnoreCase("false") || s.equalsIgnoreCase("falsch") || s.equalsIgnoreCase("nein")) {
196  AUDIO = false;
197  } else {
198  report.addCritical(function, cd, "Parameter audio not recognized: " + s);
199  }
200  }
201 
202  @Override
203  public String getDescription() {
204  String description = "This class takes a coma file and creates a zip file containing all important "
205  + "corpus file in the resources folder. It only takes exb, exs, coma, pdf and optionally mp3, "
206  + "and the folder structure. ";
207  return description;
208  }
209 
210 }
void merge(Report sr)
Definition: Report.java:73
Report zipIt(CorpusData comadata, String zipFile, Boolean AUDIO)
Definition: ZipCorpus.java:60
void addCritical(String description)
Definition: Report.java:104
void addCorrect(String statId, String description)
Definition: Report.java:217
Collection< Class<?extends CorpusData > > getIsUsableFor()
Definition: ZipCorpus.java:173
void addException(Throwable e, String description)
Definition: Report.java:287