All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nosi.core.webapp.helpers.ZipUnzipFile Maven / Gradle / Ivy

Go to download

IGRP Framework is a powerful and highly customizable platform developed by the Operational Nucleus for the Information Society (NOSi) to create web applications, it provides out of box, several modules to make easy to create stand-alone, production-grade web applications: authentication and access-control, business processes automation, reporting, page builder with automatic code generation and incorporation of the Once-Only-Principle, written in Java. IGRP Framework WAR - Contains some keys resources that give UI to IGRP Framework and others supports files.

There is a newer version: 2.0.0.241121-RCM
Show newest version
package nosi.core.webapp.helpers;

import jakarta.servlet.http.Part;
import nosi.core.webapp.import_export.FileImportAppOrPage;

import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.zip.Adler32;
import java.util.zip.CheckedInputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * @author: Emanuel Pereira
 * 31 Oct 2017
 */
public class ZipUnzipFile {


   // Extract files jar format
   public static List getZipFiles(Part file) {
      List contents = null;
      try {
         contents = new ArrayList<>();
         CheckedInputStream cis = new CheckedInputStream(file.getInputStream(), new Adler32());
         ZipInputStream zis = new ZipInputStream(new BufferedInputStream(cis));
         ZipEntry entry = null;
         while ((entry = zis.getNextEntry()) != null) {
            int order = 3;
            Charset encode = StandardCharsets.UTF_8;
            if (entry.getName().endsWith(".xml") || entry.getName().endsWith(".json") || entry.getName().endsWith(".xsl")) {
               order = 4;
            }
            if (entry.getName().startsWith("SQL/CONFIG") && entry.getName().endsWith("_ENV.xml")) {
               order = 1;
               encode = StandardCharsets.ISO_8859_1;
            }
            if (entry.getName().startsWith("SQL/CONFIG") && entry.getName().endsWith("_ACTION.xml")) {
               order = 2;
               encode = StandardCharsets.ISO_8859_1;
            }
            String ls = System.lineSeparator();
            String line = null;
            DataInputStream in = new DataInputStream(zis);
            StringBuilder content = new StringBuilder();
            BufferedReader d = new BufferedReader(new InputStreamReader(in, encode));
            while ((line = d.readLine()) != null) {
               content.append(line);
               content.append(ls);
            }

            FileImportAppOrPage f = new FileImportAppOrPage(entry.getName(), content.toString(), order);
            contents.add(f);
            zis.closeEntry();
         }
         zis.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
      Collections.sort(contents);
      return contents;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy