/*
 * The MIT License
 *
 * Copyright 2013 jglick.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package org.jenkinsci.plugins.credentialsbinding.impl;

import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
import hudson.Extension;
import hudson.FilePath;
import hudson.model.Item;
import hudson.util.FormValidation;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import org.jenkinsci.plugins.credentialsbinding.BindingDescriptor;
import org.jenkinsci.plugins.plaincredentials.FileCredentials;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;

public class ZipFileBinding extends FileBinding {

    @DataBoundConstructor public ZipFileBinding(String variable, String credentialsId) {
        super(variable, credentialsId);
    }

    @Override protected void copy(FilePath secret, FileCredentials credentials) throws IOException, InterruptedException {
        secret.unzipFrom(credentials.getContent());
    }

    @Extension public static class DescriptorImpl extends BindingDescriptor<FileCredentials> {

        @Override protected Class<FileCredentials> type() {
            return FileCredentials.class;
        }

        @Override public String getDisplayName() {
            return Messages.ZipFileBinding_secret_zip_file();
        }

        public FormValidation doCheckCredentialsId(@AncestorInPath Item owner, @QueryParameter String value) {
            for (FileCredentials c : CredentialsProvider.lookupCredentials(FileCredentials.class, owner, null, Collections.<DomainRequirement>emptyList())) {
                if (c.getId().equals(value)) {
                    try {
                        InputStream is = c.getContent();
                        byte[] data = new byte[4];
                        if (is.read(data) == 4 && data[0] == 'P' && data[1] == 'K' && data[2] == 3 && data[3] == 4) {
                            return FormValidation.ok();
                        } else {
                            return FormValidation.error("Not a ZIP file");
                        }
                    } catch (IOException x) {
                        return FormValidation.warning("Could not verify file format");
                    }
                }
            }
            return FormValidation.error("No such credentials");
        }

    }

}

@@ -1310,16 +1310,17 @@
 rement;%0A
+%0A
 import h
@@ -1417,24 +1417,25 @@
 Validation;%0A
+%0A
 import java.
@@ -1478,16 +1478,16 @@
 Stream;%0A
-
 import j
@@ -1507,16 +1507,55 @@
 ections;
+%0A%0Aimport org.apache.commons.io.IOUtils;
 %0Aimport 
@@ -2827,24 +2827,67 @@
 s(value)) %7B%0A
+                    InputStream is = null;%0A
             
@@ -2920,36 +2920,24 @@
             
-InputStream 
 is = c.getCo
@@ -3451,32 +3451,206 @@
               %7D%0A
+                    finally %7B%0A                        if (is != null) %7B%0A                            IOUtils.closeQuietly(is);%0A                        %7D%0A                    %7D%0A
                 

Fix resource leak identified by eclipse