Page 1 of 1

Auto Unzip on Upload

PostPosted:Wed Mar 12, 2014 11:35 pm
by omnicron
Is there a way to make OpenKM default feature to unzip .zip files on upload? Users never click that thing!


Thanks

Tim

Re: Auto Unzip on Upload

PostPosted:Fri Mar 14, 2014 8:25 am
by jllort
You're talking about any zip file uploaded, althought users had not clicked in unzip checkbox, automatically application unzip it internally. Is it ? Force always unzip files.

Re: Auto Unzip on Upload

PostPosted:Fri May 02, 2014 2:23 am
by omnicron
Sorry for long the delay.

Yes. Any zip file.

Re: Auto Unzip on Upload

PostPosted:Sat May 03, 2014 4:25 pm
by jllort
Could be done creating a simply Automation task based on document creation event. Also take a look at FileUploadServlet.java class where are the private methods what are doing it. The method in what you must be interested is importZip, inpire on it for creating your Automation action. Read here http://wiki.openkm.com/index.php/Extend_automation_6.0 and here http://doxygen.openkm.com/openkm/dc/df7 ... ation.html ( from openkm 6.3 can create a jar file and simply copy into /tomcat/plugins
Code: Select all
private synchronized String importZip(String path, InputStream is) throws PathNotFoundException, ItemExistsException,
			AccessDeniedException, RepositoryException, IOException, DatabaseException, ExtensionException, AutomationException {
		log.debug("importZip({}, {})", path, is);
		java.io.File tmpIn = null;
		java.io.File tmpOut = null;
		String errorMsg = null;
		
		try {
			// Create temporal
			tmpIn = File.createTempFile("okm", ".zip");
			tmpOut = FileUtils.createTempDir();
			FileOutputStream fos = new FileOutputStream(tmpIn);
			IOUtils.copy(is, fos);
			fos.close();
			
			// Unzip files
			File fileTmpIn = new File(tmpIn);
			fileTmpIn.archiveCopyAllTo(tmpOut);
			File.umount();
			
			// Import files
			StringWriter out = new StringWriter();
			ImpExpStats stats = RepositoryImporter.importDocuments(null, tmpOut, path, false, false, false, out, new TextInfoDecorator(
					tmpOut));
			
			if (!stats.isOk()) {
				errorMsg = out.toString();
			}
			
			out.close();
		} catch (IOException e) {
			log.error("Error importing zip", e);
			throw e;
		} finally {
			IOUtils.closeQuietly(is);
			
			if (tmpIn != null) {
				org.apache.commons.io.FileUtils.deleteQuietly(tmpIn);
			}
			
			if (tmpOut != null) {
				org.apache.commons.io.FileUtils.deleteQuietly(tmpOut);
			}
		}
		
		log.debug("importZip: {}", errorMsg);
		return errorMsg;
	}