package org.nd4j.linalg.io;

import org.apache.commons.io.IOUtils;

import java.io.*;
import java.net.URL;

/**
 * A slightly upgraded version of spring's
 * classpath resource
 *
 *
 */
public class ClassPathResource extends AbstractFileResolvingResource {
    private final String path;
    private ClassLoader classLoader;
    private Class<?> clazz;

    public ClassPathResource(String path) {
        this(path, (ClassLoader) null);
    }

    public ClassPathResource(String path, ClassLoader classLoader) {
        Assert.notNull(path, "Path must not be null");
        String pathToUse = StringUtils.cleanPath(path);
        if(pathToUse.startsWith("/")) {
            pathToUse = pathToUse.substring(1);
        }

        this.path = pathToUse;
        this.classLoader = classLoader != null?classLoader:ClassUtils.getDefaultClassLoader();
    }

    public ClassPathResource(String path, Class<?> clazz) {
        Assert.notNull(path, "Path must not be null");
        this.path = StringUtils.cleanPath(path);
        this.clazz = clazz;
    }

    protected ClassPathResource(String path, ClassLoader classLoader, Class<?> clazz) {
        this.path = StringUtils.cleanPath(path);
        this.classLoader = classLoader;
        this.clazz = clazz;
    }

    public final String getPath() {
        return this.path;
    }

    public final ClassLoader getClassLoader() {
        return this.classLoader != null ? this.classLoader:this.clazz.getClassLoader();
    }



    /**
     * Get a temp file from the classpath.
     * This is for resources where
     * a file is needed and the
     * classpath resource is in a jar file
     * @return the temp file
     * @throws IOException
     */
    public File getTempFileFromArchive() throws IOException {
        InputStream is = getInputStream();
        File tmpFile = new File(path + "tmp");
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(tmpFile));
        tmpFile.deleteOnExit();
        IOUtils.copy(is,bos);
        bos.flush();
        bos.close();
        return tmpFile;

    }

    public boolean exists() {
        URL url;
        if(this.clazz != null) {
            url = this.clazz.getResource(this.path);
        } else {
            url = this.classLoader.getResource(this.path);
        }

        return url != null;
    }

    public InputStream getInputStream() throws IOException {
        InputStream is;
        if(this.clazz != null) {
            is = this.clazz.getResourceAsStream(this.path);
        } else {
            is = this.classLoader.getResourceAsStream(this.path);
        }

        if(is == null) {
            throw new FileNotFoundException(this.getDescription() + " cannot be opened because it does not exist");
        } else {
            return is;
        }
    }

    public URL getURL() throws IOException {
        URL url;
        if(this.clazz != null) {
            url = this.clazz.getResource(this.path);
        } else {
            url = this.classLoader.getResource(this.path);
        }

        if(url == null) {
            throw new FileNotFoundException(this.getDescription() + " cannot be resolved to URL because it does not exist");
        } else {
            return url;
        }
    }

    public Resource createRelative(String relativePath) {
        String pathToUse = StringUtils.applyRelativePath(this.path, relativePath);
        return new ClassPathResource(pathToUse, this.classLoader, this.clazz);
    }

    public String getFilename() {
        return StringUtils.getFilename(this.path);
    }

    public String getDescription() {
        StringBuilder builder = new StringBuilder("class path resource [");
        String pathToUse = this.path;
        if(this.clazz != null && !pathToUse.startsWith("/")) {
            builder.append(ClassUtils.classPackageAsResourcePath(this.clazz));
            builder.append('/');
        }

        if(pathToUse.startsWith("/")) {
            pathToUse = pathToUse.substring(1);
        }

        builder.append(pathToUse);
        builder.append(']');
        return builder.toString();
    }

    public boolean equals(Object obj) {
        if(obj == this) {
            return true;
        } else if(!(obj instanceof ClassPathResource)) {
            return false;
        } else {
            ClassPathResource otherRes = (ClassPathResource)obj;
            return this.path.equals(otherRes.path) && ObjectUtils.nullSafeEquals(this.classLoader, otherRes.classLoader) && ObjectUtils.nullSafeEquals(this.clazz, otherRes.clazz);
        }
    }

    public int hashCode() {
        return this.path.hashCode();
    }
}

@@ -1819,16 +1819,222 @@
    File 
+tmpFile;%0A        if(path.contains(%22/%22) %7C%7C path.contains(%22%5C%5C%22))%7B%0A            int idx = Math.max(path.lastIndexOf(%22/%22),path.lastIndexOf(%22%5C%5C%22));%0A            String subpath = path.substring(idx+1);%0A            
 tmpFile 
@@ -2048,22 +2048,128 @@
 ile(
+sub
 path
- + %22tmp%22);
++%22tmp%22);%0A        %7D else %7B%0A            tmpFile = new File(path+%22tmp%22);%0A        %7D%0A        tmpFile.deleteOnExit();%0A%0A
 %0A   
@@ -2261,39 +2261,8 @@
 ));%0A
-        tmpFile.deleteOnExit();
 %0A   

Change how ClassPathResource handles paths with directories for extracting