Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.xmlcalabash.extensions;
  
  
  import java.net.URI;
 import java.net.URL;
 

Author(s):
ndw
 
 public class Unzip extends DefaultStep {
     protected final static QName _href = new QName("""href");
     protected final static QName _content_type = new QName("""content-type");
     protected final static QName c_zipfile = new QName("c"."zipfile");
     protected final static QName c_file = new QName("c"."file");
     protected final static QName _file = new QName("""file");
     protected final static QName _charset = new QName("""charset");
     protected final static QName _name = new QName("""name");
     protected final static QName c_directory = new QName("c"."directory");
     protected final static QName _compressed_size = new QName("""compressed-size");
     protected final static QName _comment = new QName("""comment");
     protected final static QName _size = new QName("""size");
     protected final static QName _date = new QName("""date");
 
     private WritablePipe result = null;
     private String zipFn = null;
     private URI zipURI = null;
     private String name = null;
     private String contentType = "application/xml";
     private String charset = null;

    
Creates a new instance of Unzip
 
     public Unzip(XProcRuntime runtimeXAtomicStep step) {
         super(runtime,step);
     }
 
     public void setOutput(String portWritablePipe pipe) {
          = pipe;
     }
 
     public void reset() {
         .resetWriter();
     }
 
     public void run() throws SaxonApiException {
         super.run();
         
          = getOption().getString();
          = getOption().getBaseURI();
 
         if (getOption() != null) {
              = getOption().getString();
         }
 
         if (getOption() != null) {
              = getOption().getString();
         }
 
         if (getOption() != null) {
              = getOption().getString();
         }
 
         try {
             URL url = .resolve().toURL();
             URLConnection connection = url.openConnection();
             InputStream stream = connection.getInputStream();
 
             ZipInputStream zipFile = new ZipInputStream(stream);
 
             TreeWriter tree = new TreeWriter();
 
             if ( == null) {
                 tree.startDocument(.getNode().getBaseURI());
                 tree.addStartElement();
                tree.addAttribute(url.toString());
                tree.startContent();
                DatatypeFactory dfactory = DatatypeFactory.newInstance();
                GregorianCalendar cal = new GregorianCalendar();
                ZipEntry entry = zipFile.getNextEntry();
                while (entry != null) {
                    cal.setTimeInMillis(entry.getTime());
                    XMLGregorianCalendar xmlCal = dfactory.newXMLGregorianCalendar(cal);
                    if (entry.isDirectory()) {
                        tree.addStartElement();
                    } else {
                        tree.addStartElement();
                        tree.addAttribute(""+entry.getCompressedSize());
                        tree.addAttribute(""+entry.getSize());
                    }
                    if (entry.getComment() != null) {
                        tree.addAttribute(entry.getComment());
                    }
                    tree.addAttribute(""+entry.getName());
                    tree.addAttribute(xmlCal.toXMLFormat());
                    tree.startContent();
                    tree.addEndElement();
                    entry = zipFile.getNextEntry();
                }
                zipFile.close();
                tree.addEndElement();
                tree.endDocument();
                .write(tree.getResult());
            } else {
                ZipEntry entry = zipFile.getNextEntry();
                while (entry != null) {
                    if (.equals(entry.getName())) {
                        break;
                    }
                    entry = zipFile.getNextEntry();
                }
                if (entry == null) {
                    throw new XProcException(.getNode(), "ZIP file does not contain '" +  + "'");
                }
                if ("application/xml".equals() || "text/xml".equals()
                        || .endsWith("+xml")) {
                    InputSource isource = new InputSource(zipFile);
                    XdmNode doc = .parse(isource);
                    .write(doc);
                } else {
                    boolean storeText = ( != null && .startsWith("text/") &&  != null);
                    tree.startDocument(.getNode().getBaseURI());
                    tree.addStartElement(.);
                    tree.addAttribute(,);
                    tree.addAttribute();
                    if (!storeText) {
                        tree.addAttribute("base64");
                    }
                    tree.startContent();
                    if (storeText) {
                        InputStreamReader reader = new InputStreamReader(zipFile);
                        int maxlen = 4096;
                        char[] chars = new char[maxlen];
                        int read = reader.read(chars, 0, maxlen);
                        while (read >= 0) {
                            if (read > 0) {
                                String s = new String(chars);
                                tree.addText(s);
                            }
                            read = reader.read(chars, 0, maxlen);
                        }
                        reader.close();
                    } else {
                        BufferedInputStream bufstream = new BufferedInputStream(zipFile);
                        int maxlen = 4096 * 3;
                        byte[] bytes = new byte[maxlen];
                        int read = bufstream.read(bytes, 0, maxlen);
                        while (read >= 0) {
                            if (read > 0) {
                                String base64 = Base64.encodeBytes(bytes, 0, read);
                                tree.addText(base64 + "\n");
                            }
                            read = bufstream.read(bytes, 0, maxlen);
                        }
                        bufstream.close();
                    }
                    tree.addEndElement();
                    tree.endDocument();
                    .write(tree.getResult());
                }
                zipFile.close();
            }
        } catch (MalformedURLException mue) {
            throw new XProcException(.mue);
        } catch (IOException ioe) {
            throw new XProcException(.ioe);
        } catch (DatatypeConfigurationException dce) {
            throw new XProcException(.dce);
        }
    }
New to GrepCode? Check out our FAQ X