---
title: Validating zip files in Mendix
description: Validate zip files on upload in Mendix to prevent errors. The Java Action identifies damaged files without extraction, ensuring smooth user experience.
image: https://www.epiuse.com/hubfs/00-EPI-USE/11-Mendix/00-blogs/20240418-validating-zip-files/Validating-zip-files.jpeg
---

# Validating zip files in Mendix

Oct 26, 2023 1:15:00 PM | Ralph Viktor

[Back to blog](https://www.epiuse.com/mendix/articles/all)

A while ago I ran into a situation where our users were sometimes uploading zip files that were damaged. I was asked to look into a way to validate the zip file in Mendix when it is uploaded so we can provide immediate feedback to our users.

### Introduction

A while ago I ran into a situation where our users were sometimes uploading zip files that were damaged. The zip file could still be opened with a tool like [7zip](https://7-zip.org/?ref=ninetaillabs.com), but when an attempt is made to extract the documents from the zip file an error would be raised. In most cases all but one of the files could be extracted pointing to a damaged stream inside the zip. While we could ask our users to validate the zip files before they upload them this is an inconvenience to them so I was asked to look into a way to validate the zip file in Mendix when it is uploaded so we can provide immediate feedback to our users.

![DamagedZip](https://www.epiuse.com/hs-fs/hubfs/00-EPI-USE/11-Mendix/00-blogs/20240418-validating-zip-files/DamagedZip.png?width=850&height=556&name=DamagedZip.png)  
*CRC failure on zip extraction*

### Design

I first went to the Mendix Marketplace to see if there was an existing widget or module that already address this specific issue, however the only modules available are for opening or creating zip files. A simple solution would be to extract the zip file and see if it succeeds, however this will result in files being created somewhere that would then have to be deleted which is inefficient, so instead I decided to create a Java action and then implement some logic to validate the file without having to extract it.

### Implementation

**Mendix Java Action **

The first step is to define the Java action in Mendix. I simply created a Java action named *ValidateZipFile* and set up a single parameter that passes a *System.FileDocument* as this will allow us to pass any type of document in our project to the action.

![DamagedZip](https://www.epiuse.com/hs-fs/hubfs/00-EPI-USE/11-Mendix/00-blogs/20240418-validating-zip-files/Java-parameter-definition%20.png?width=850&height=556&name=Java-parameter-definition%20.png)  
*Java parameter definition*

For the return type of the action we simply set it to a boolean and give it an appropriate name.

![Java-action-return-type](https://www.epiuse.com/hs-fs/hubfs/00-EPI-USE/11-Mendix/00-blogs/20240418-validating-zip-files/Java-action-return-type.png?width=363&height=143&name=Java-action-return-type.png)  
*  
Java action return type*

**Java Code **

The code to validate a zip file is actually fairly simple. While searching I have found some rather complex solutions, however I decided to go for the simplest solution I could create that would still successfully validate the file. Below is the method I created to handle the validation.

![Java-Code](https://www.epiuse.com/hs-fs/hubfs/00-EPI-USE/11-Mendix/00-blogs/20240418-validating-zip-files/Java-Code.png?width=1284&height=1406&name=Java-Code.png)

The method takes an *InputStream* for our zip file and then uses a *ZipInputStream* from *java.util.zip* so we can read the stream as a zip file. We then grab the first *ZipEntry* from the stream, if we don't get a *ZipEntry* it means that the stream cannot be read as a zip so the file is probably damaged.

If we do get a *ZipEntry* we then enter a while loop that will keep running while our *ze* variable is not null. For each *ZipEntry* we then get the CRC, the compressed size and the name of the entry. If any of these fail a *ZipException* will be thrown and we know that our file is not valid. If no error occurs we grab the next *ZipEntry* from the stream and repeat the process. Once we have gone through all of the entries the *getNextEntry* method will return null and we will break out of the loop. If we get to this point we know that zip file is valid, so we return true. Finally we will simply close the *ZipInputStream*.

![ZipInputStream](https://www.epiuse.com/hs-fs/hubfs/00-EPI-USE/11-Mendix/00-blogs/20240418-validating-zip-files/ZipInputStream.png?width=1284&height=542&name=ZipInputStream.png)

The above code is placed directly in the executeAction method that Mendix generated for our Java Action. The __*File variable* is simply the *IMendixObject* that Mendix provides for our parameter that we can use to get the *InputStream* for our file. We then invoke the *isValid* method defined above for the stream and returns the result to Mendix.

### Conclusion

Validating a zip file without extracting it is pretty straightforward using the code outlined above. I tested the logic using most of the damaged zip files we received and it was able to identify each of them correctly. The Java Action is generic and can easily be included into a reusable module.

### Recent Articles

#### [Transforming the Shop Floor with Low-Code and AI Solutions](https://www.epiuse.com/mendix/articles/transforming-the-shop-floor-with-low-code-and-ai-solutions)

#### [Leveraging Low-Code and AI to Revolutionize Change Management in Manufacturing](https://www.epiuse.com/mendix/articles/leveraging-low-code-and-ai-to-revolutionize-change-management-in-manufacturing)

#### [Bridging the Gap Between Manufacturing and Sales with Low-Code and AI Solutions](https://www.epiuse.com/mendix/articles/bridging-the-gap-between-manufacturing-and-sales-with-low-code-and-ai-solutions)

#### [RAD AI – Ignite | Episode 3: Extract and Organize Data](https://www.epiuse.com/mendix/articles/rad-ai-ignite-series-episode-3-extract-and-organize-data)

#### [RAD AI – Ignite | Episode 2: Identify and Address Anomalies](https://www.epiuse.com/mendix/articles/rad-ai-ignite-series-episode-2-identify-and-address-anomalies)

### Get instant updates

### Get instant updates

```json
{
  "@context" : "https://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "Ralph Viktor",
    "url" : "https://www.epiuse.com/mendix/articles/author/ralph-viktor"
  },
  "dateModified" : "2024-04-18T08:26:01.832Z",
  "datePublished" : "2023-10-26T17:15:00.000Z",
  "headline" : "Validating zip files in Mendix",
  "image" : [ "https://www.epiuse.com/hubfs/00-EPI-USE/11-Mendix/00-blogs/20240418-validating-zip-files/Validating-zip-files.jpeg" ],
  "mainEntityOfPage" : {
    "@id" : "https://www.epiuse.com/mendix/articles/validating-zip-files-in-mendix",
    "@type" : "WebPage"
  },
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "url" : "https://www.epiuse.com/hubfs/00-EPI-USE/00-Favicon/android-chrome-512x512.png"
    },
    "name" : "EU Labs Limited"
  }
}
```