Skip to content

Commit 59d0e1f

Browse files
committed
added simple zipping, if an manifest.xml exists in git
1 parent 7d53906 commit 59d0e1f

1 file changed

Lines changed: 113 additions & 1 deletion

File tree

src/main/java/de/unirostock/sems/cbarchive/web/importer/GitImporter.java

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package de.unirostock.sems.cbarchive.web.importer;
22

33
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.FileOutputStream;
46
import java.io.IOException;
57
import java.io.InputStream;
8+
import java.io.OutputStream;
69
import java.net.URL;
710
import java.nio.file.Path;
811
import java.text.ParseException;
@@ -13,6 +16,8 @@
1316
import java.util.List;
1417
import java.util.regex.Matcher;
1518
import java.util.regex.Pattern;
19+
import java.util.zip.ZipEntry;
20+
import java.util.zip.ZipOutputStream;
1621

1722
import javax.xml.transform.TransformerException;
1823

@@ -69,7 +74,19 @@ public GitImporter importRepo() throws ImporterException {
6974
remoteUrl = processNzRepoLink( remoteUrl );
7075

7176
cloneGit();
72-
buildArchive();
77+
78+
// check for existing manifest.xml
79+
File manifest = new File(tempDir, "manifest.xml");
80+
if( manifest.exists() && manifest.length() > 0 ) {
81+
// there is a manifest.xml
82+
// -> zip it and check if valid CombineArchive
83+
zipArchive();
84+
}
85+
else {
86+
// no manifest.xml found
87+
// -> build Archive from scratch
88+
buildArchive();
89+
}
7390

7491
return this;
7592
}
@@ -198,6 +215,101 @@ private OmexDescription getOmexForFile( Path relativePath ) throws ImporterExcep
198215
);
199216
}
200217

218+
private void zipArchive() throws ImporterException {
219+
220+
try {
221+
tempFile = File.createTempFile(Fields.TEMP_FILE_PREFIX, ".omex");
222+
tempFile.delete(); // delete tmp file, if it exists already
223+
224+
// do it!
225+
zipIt(tempFile, tempDir);
226+
227+
// try to open it as CA
228+
CombineArchive archive = null;
229+
try {
230+
archive = new CombineArchive(tempFile, true);
231+
// check for errors
232+
if( archive.hasErrors() ) {
233+
List<String> errors = archive.getErrors();
234+
StringBuilder errorString = new StringBuilder("Errors while parsing the manifest.xml:\n");
235+
for( String error : errors ) {
236+
errorString.append("- ");
237+
errorString.append(error);
238+
errorString.append("\n");
239+
}
240+
LOGGER.warn( errorString.toString() );
241+
throw new ImporterException( errorString.toString() );
242+
}
243+
244+
} catch (IOException | JDOMException | ParseException | CombineArchiveException e) {
245+
LOGGER.error(e, "Cannot open zipped CombineArchive!");
246+
throw new ImporterException("Cannot open zipped CombineArchive!", e);
247+
} finally {
248+
if( archive != null )
249+
archive.close();
250+
}
251+
252+
} catch(IOException e) {
253+
LOGGER.error(e, "IOException while build CombineArchive from Git Repository");
254+
throw new ImporterException(e);
255+
}
256+
257+
}
258+
259+
private void zipIt(File output, File baseDir) throws ImporterException {
260+
261+
Path basePath = baseDir.toPath();
262+
List<File> fileList = new LinkedList<File>();
263+
OutputStream fileOutput = null;
264+
ZipOutputStream zipOutput = null;
265+
266+
try {
267+
// list directory content
268+
generateFileList(baseDir, fileList);
269+
270+
// prepare output streams
271+
fileOutput = new FileOutputStream(output);
272+
zipOutput = new ZipOutputStream(fileOutput);
273+
274+
for( File file : fileList ) {
275+
// add entry
276+
ZipEntry entry = new ZipEntry( file.toPath().relativize(basePath).toString() );
277+
zipOutput.putNextEntry(entry);
278+
279+
// copy data
280+
InputStream entryInput = new FileInputStream(file);
281+
IOUtils.copy(entryInput, zipOutput);
282+
283+
entryInput.close();
284+
zipOutput.closeEntry();
285+
}
286+
287+
} catch (IOException e) {
288+
LOGGER.error(e, "Cannot zip folder");
289+
throw new ImporterException("Cannot zip folder", e);
290+
} finally {
291+
IOUtils.closeQuietly(zipOutput);
292+
IOUtils.closeQuietly(fileOutput);
293+
}
294+
295+
}
296+
297+
private void generateFileList(File node, List<File> fileList) {
298+
299+
// node is a file -> just add it
300+
if( node.isFile() ) {
301+
fileList.add(node);
302+
}
303+
else if( node.isDirectory() ) {
304+
// node is a directory -> dive into
305+
String[] subNode = node.list();
306+
for( String name : subNode ) {
307+
generateFileList( new File(node, name), fileList );
308+
}
309+
}
310+
311+
}
312+
201313
private String processNzRepoLink (String link) throws ImporterException {
202314

203315
/*

0 commit comments

Comments
 (0)