cancel
Showing results for 
Search instead for 
Did you mean: 

unzip a file after upload

roeland19
Champ in-the-making
Champ in-the-making
Is there a way i can directly unzip a zipped file after i upload it ? I have
searched for some examples but i couldn't find any.
18 REPLIES 18

doc
Champ in-the-making
Champ in-the-making
Hi,

If you are talking about ACP file.
You need only to use import function in the space where you plan to import "ACP" file.

davidc
Star Contributor
Star Contributor
We have considered providing an action to explode native zip files into spaces and files.  Or you can develop your own action to do this with the SDK and contribute it to the community.

roeland19
Champ in-the-making
Champ in-the-making
I'll give it a try. I'm not quite sure how to do this because i've only been programming in alfresco for about a week so any advise would be helpful. I'll keep you posted if i figure something out.

davidc
Star Contributor
Star Contributor
There are two routes.  First, you can develop a Javascript action.  However, the processing of a zip file might be a problem in Javascript.  So, you can drop down and develop an action in Java.  For this, you can use the Alfresco SDK which provides an Eclipse example.

roeland19
Champ in-the-making
Champ in-the-making
Is there a way to extract this the same way you extract acp files? When i rename a zip file to a acp file and aply the rule i get this error

A system error happened during the operation: Transaction didn't commit: Failed to find xml meta-data file within .acp package

Is it hard to add this xml meta-data?

davidc
Star Contributor
Star Contributor
That's not going to work.  The ACP import understands how to import and expand an ACP file.

A custom zip file import and expand action will have to be written.

roeland19
Champ in-the-making
Champ in-the-making
i have made an action and an executor but when I try this i get this error:

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

and what does "importerService" do?

this is mij executor


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)));
   }

}

and this is my action


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;
            }
         }   
}

rwetherall
Confirmed Champ
Confirmed Champ
Hi,

The error you are seeing here is produced by the action framework.  It is telling you that a mandatory attribute of the action has not been set.

Both the destination folder and encoding parameters of this action have been defined as mandatory in the addParameterDefinitions method.  It is the 3rd parameter in the ParameterDefintionImpl that indicates that they are mandatory.

To resolve the problem you could set these parameters to be non-mandatory (this may not make sense as they sound like they should be mandatory) or ensure that code that executes the action has values for these parameters set.

Where are you trying to execute the action from when you encounter the error?

The importer service is an entry point for importing xml data sources into the repository.  From scanning the code I could not see where you are using it.  What are you intending to do with it?

Cheers,
Roy

roeland19
Champ in-the-making
Champ in-the-making
Greetings,

I took as an example the executor and the action from the acp import. I can set this Rule by selecting as a condition "items with the specified mime type"
then i select zip and choose next. then i set my custom action and get the same form from when you set the acp import rule where you can select the destination.
Then i give it a name and the rule is set. But when i upload a zip file i get the error. Thats alsow the reason the importer service is in my code. I thought i could use it
to like the acp importer uses it. But if it is used for xml code i don't think i can use it.


    My regards
Getting started

Tags


Find what you came for

We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.