As we transition to the new Hyland Community, some post links may be temporarily disrupted as we finalize redirects. We apologize for the inconvenience and are working to resolve this as quickly as possible. Details.
A system error happened during the operation: Transaction didn't commit: Failed to find xml meta-data file within .acp package
A system error happened during the operation: Transaction didn't commit: A value for the mandatory parameter destination has not been set on the rule item unzip
public class ImporterActionExecuter extends ActionExecuterAbstractBase { public static final String NAME = "unzip"; public static final String PARAM_ENCODING = "encoding"; public static final String PARAM_DESTINATION_FOLDER = "destination"; private static final String TEMP_FILE_PREFIX = "alf"; private static final String TEMP_FILE_SUFFIX = ".zip"; /** * The importer service */ private ImporterService importerService; /** * The node service */ private NodeService nodeService; /** * The content service */ private ContentService contentService; /** * Sets the ImporterService to use * * @param importerService The ImporterService */ public void setImporterService(ImporterService importerService) { this.importerService = importerService; } /** * Sets the NodeService to use * * @param nodeService The NodeService */ public void setNodeService(NodeService nodeService) { this.nodeService = nodeService; } /** * Sets the ContentService to use * * @param contentService The ContentService */ public void setContentService(ContentService contentService) { this.contentService = contentService; } /** * @see org.alfresco.repo.action.executer.ActionExecuter#execute(org.alfresco.repo.ref.NodeRef, org.alfresco.repo.ref.NodeRef) */ public void executeImpl(Action ruleAction, NodeRef actionedUponNodeRef) { if (this.nodeService.exists(actionedUponNodeRef) == true) { // The node being passed in should be an Alfresco content package ContentReader reader = this.contentService.getReader(actionedUponNodeRef, ContentModel.PROP_CONTENT); if (reader != null) { if (MimetypeMap.MIMETYPE_ZIP.equals(reader.getMimetype())) { File zipFile = null; try { // unfortunately a ZIP file can not be read directly from an input stream so we have to create // a temporary file first zipFile = TempFileProvider.createTempFile(TEMP_FILE_PREFIX, TEMP_FILE_SUFFIX); reader.getContent(zipFile); new ZipImportAction().unzip(zipFile.getPath()); } finally { // now the import is done, delete the temporary file if (zipFile != null) { zipFile.delete(); } } } } } } /** * @see org.alfresco.repo.action.ParameterizedItemAbstractBase#addParameterDefinitions(java.util.List) */ protected void addParameterDefinitions(List<ParameterDefinition> paramList) { paramList.add(new ParameterDefinitionImpl(PARAM_DESTINATION_FOLDER, DataTypeDefinition.NODE_REF, true, getParamDisplayLabel(PARAM_DESTINATION_FOLDER))); paramList.add(new ParameterDefinitionImpl(PARAM_ENCODING, DataTypeDefinition.TEXT, true, getParamDisplayLabel(PARAM_ENCODING))); } }
public class ZipImportAction { public void copyInputStream(InputStream in, OutputStream out) throws IOException { byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) >= 0) out.write(buffer, 0, len); in.close(); out.close(); } public void unzip(String file) { Enumeration entries; ZipFile zipFile; if (file.length() == 0) { System.err.println("Usage: Unzip zipfile"); return; } try { zipFile = new ZipFile(file); entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = (ZipEntry) entries.nextElement(); if (entry.isDirectory()) { // Assume directories are stored parents first then // children. System.err.println("Extracting directory: " + entry.getName()); // make a new directory (new File(entry.getName())).mkdir(); continue; } System.err.println("Extracting file: " + entry.getName()); copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream(new FileOutputStream(entry .getName()))); } zipFile.close(); } catch (IOException ioe) { System.err.println("Unhandled exception:"); ioe.printStackTrace(); return; } } }
package org.alfresco.repo.action.executer; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.Serializable; import java.util.Enumeration; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.zip.ZipException; import org.alfresco.model.ContentModel; import org.alfresco.repo.action.ParameterDefinitionImpl; import org.alfresco.repo.action.executer.ActionExecuterAbstractBase; import org.alfresco.repo.content.MimetypeMap; import org.alfresco.service.cmr.action.Action; import org.alfresco.service.cmr.action.ParameterDefinition; import org.alfresco.service.cmr.dictionary.DataTypeDefinition; import org.alfresco.service.cmr.dictionary.InvalidAspectException; import org.alfresco.service.cmr.dictionary.InvalidTypeException; import org.alfresco.service.cmr.model.FileExistsException; import org.alfresco.service.cmr.model.FileFolderService; import org.alfresco.service.cmr.model.FileInfo; import org.alfresco.service.cmr.repository.ChildAssociationRef; import org.alfresco.service.cmr.repository.ContentIOException; import org.alfresco.service.cmr.repository.ContentReader; import org.alfresco.service.cmr.repository.ContentService; import org.alfresco.service.cmr.repository.ContentWriter; import org.alfresco.service.cmr.repository.InvalidNodeRefException; import org.alfresco.service.cmr.repository.MimetypeService; import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeService; import org.alfresco.service.namespace.InvalidQNameException; import org.alfresco.service.namespace.NamespaceService; import org.alfresco.service.namespace.QName; import org.alfresco.util.TempFileProvider; import org.alfresco.web.bean.repository.Repository; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipFile; /** * Zip action executor * * @author Davide Taibi */ public class ZipActionExecuter extends ActionExecuterAbstractBase { public static final String NAME = "importzip"; public static final String PARAM_ENCODING = "encoding"; public static final String PARAM_DESTINATION_FOLDER = "destination"; private static final String TEMP_FILE_PREFIX = "alf"; private static final String TEMP_FILE_SUFFIX = ".zip"; /** * The node service */ private NodeService nodeService; /** * The content service */ private ContentService contentService; private MimetypeService mimetypeService; private FileFolderService fileFolderService; /** * Sets the NodeService to use * * @param nodeService The NodeService */ public void setNodeService(NodeService nodeService) { this.nodeService = nodeService; } /** * Sets the ContentService to use * * @param contentService The ContentService */ public void setContentService(ContentService contentService) { this.contentService = contentService; } public void setFileFolderService(FileFolderService fileFolderService) { this.fileFolderService = fileFolderService; } public void setMimetypeService(MimetypeService mimetypeService) { this.mimetypeService = mimetypeService; } private void extractFile(ZipFile archive,String mExtractToDir){ String fileName; String destFileName; byte[] buffer = new byte[16384]; mExtractToDir=mExtractToDir+File.separator; try { for ( Enumeration e = archive.getEntries(); e.hasMoreElements(); ) { ZipEntry entry = (ZipEntry)e.nextElement(); if ( ! entry.isDirectory() ) { fileName = entry.getName(); fileName = fileName.replace('/', File.separatorChar); destFileName = mExtractToDir + fileName; File destFile = new File(destFileName); String parent = destFile.getParent(); if ( parent != null ) { File parentFile = new File(parent); if ( ! parentFile.exists() ) parentFile.mkdirs(); } InputStream in = archive.getInputStream(entry); OutputStream out = new FileOutputStream(destFileName); int count; while ( (count = in.read(buffer)) != -1 ) out.write(buffer, 0, count ); in.close(); out.close(); }else{ File newdir = new File( mExtractToDir + entry.getName() ); newdir.mkdir(); } } } catch (ZipException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } private void deleteDir(File dir){ File elenco=new File(dir.getPath()); for (File file:elenco.listFiles()){ if (file.isFile()) file.delete(); else deleteDir(file); } dir.delete(); } public void executeImpl(Action ruleAction, NodeRef actionedUponNodeRef) { String spaceType; NodeRef parentNodeRef; FileInfo fileInfo; NodeRef nodeRef; if (this.nodeService.exists(actionedUponNodeRef) == true) { ContentReader reader = this.contentService.getReader( actionedUponNodeRef, ContentModel.PROP_CONTENT); if (reader != null) { if (MimetypeMap.MIMETYPE_ZIP.equals(reader.getMimetype())) { File zFile = null; ZipFile zipFile = null; try { spaceType = ContentModel.TYPE_FOLDER.toString(); String dirName = ""+this.nodeService.getProperty(actionedUponNodeRef, ContentModel.PROP_NAME); dirName=dirName.substring(0,dirName.indexOf(".zip")); parentNodeRef = (NodeRef)ruleAction.getParameterValue(PARAM_DESTINATION_FOLDER);// this.nodeService.getRootNode(Repository.getStoreRef()); fileInfo = fileFolderService.create( parentNodeRef, dirName, Repository.resolveToQName(spaceType)); nodeRef = fileInfo.getNodeRef(); zFile = TempFileProvider.createTempFile( TEMP_FILE_PREFIX, TEMP_FILE_SUFFIX); reader.getContent(zFile); zipFile = new ZipFile(zFile, "Cp437"); File tempDir=new File(TempFileProvider.getTempDir().getPath()+File.separator+dirName); if (tempDir.exists()) deleteDir(tempDir); extractFile(zipFile,tempDir.getPath()); importFile(tempDir.getPath(),nodeRef); deleteDir(tempDir); } catch (ContentIOException e) { e.printStackTrace(); } catch (InvalidNodeRefException e) { e.printStackTrace(); } catch (FileExistsException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } } } private void importFile(String dir,NodeRef root){ File elenco=new File(dir); for (File file:elenco.listFiles()){ try { if (file.isFile()){ InputStream contentStream = new FileInputStream(file); // assign name String fileName=file.getName(); Map<QName, Serializable> contentProps = new HashMap<QName, Serializable>(); contentProps.put(ContentModel.PROP_NAME, fileName); // create content node ChildAssociationRef association = nodeService .createNode( root, ContentModel.ASSOC_CONTAINS, QName.createQName( NamespaceService.CONTENT_MODEL_PREFIX, fileName), ContentModel.TYPE_CONTENT, contentProps); NodeRef content = association.getChildRef(); // add titled aspect (for Web Client display) Map<QName, Serializable> titledProps = new HashMap<QName, Serializable>(); titledProps.put(ContentModel.PROP_TITLE, fileName); titledProps.put(ContentModel.PROP_DESCRIPTION,fileName); nodeService.addAspect(content, ContentModel.ASPECT_TITLED, titledProps); ContentWriter writer = contentService.getWriter(content, ContentModel.PROP_CONTENT, true); writer.setMimetype(mimetypeService.guessMimetype(file.getAbsolutePath())); writer.setEncoding("Cp437"); writer.putContent(contentStream); }else{ String spaceType = ContentModel.TYPE_FOLDER.toString(); FileInfo fileInfo = fileFolderService.create( root, file.getName(), Repository.resolveToQName(spaceType)); importFile(file.getPath(),fileInfo.getNodeRef()); } } catch (InvalidTypeException e) { e.printStackTrace(); } catch (InvalidAspectException e) { e.printStackTrace(); } catch (InvalidQNameException e) { e.printStackTrace(); } catch (ContentIOException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (InvalidNodeRefException e) { e.printStackTrace(); } catch (FileExistsException e) { e.printStackTrace(); } } } protected void addParameterDefinitions(List<ParameterDefinition> paramList) { paramList.add(new ParameterDefinitionImpl(PARAM_DESTINATION_FOLDER, DataTypeDefinition.NODE_REF, true, getParamDisplayLabel(PARAM_DESTINATION_FOLDER))); paramList.add(new ParameterDefinitionImpl(PARAM_ENCODING, DataTypeDefinition.TEXT, true, getParamDisplayLabel(PARAM_ENCODING))); } }
importzip.title=Import Zip File importzip.description=This Action import zip file into alfresco importzip.aspect-name.display-label=The name of the aspect to apply to the node.
<bean id="importzip" class="org.alfresco.repo.action.executer.ZipActionExecuter" parent="action-executer"> <property name="nodeService"> <ref bean="NodeService"></ref> </property> <property name="contentService"> <ref bean="ContentService" /> </property> <property name="mimetypeService"> <ref bean="MimetypeService"></ref> </property> <property name="fileFolderService"> <ref bean="FileFolderService"></ref> </property> </bean> <bean id="importzip-messages" class="org.alfresco.i18n.ResourceBundleBootstrapComponent"> <property name="resourceBundles"> <list> <value>org.alfresco.repo.action.importzip-action-messages</value> </list> </property> </bean>
<config evaluator="string-compare" condition="Action Wizards"> <action-handlers> <handler name="importzip" class="org.alfresco.web.bean.actions.handlers.ImportHandler" /> </action-handlers> </config>