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

org.zeroturnaround.javarebel.integration.streambase.JarUtil Maven / Gradle / Ivy

The newest version!
package org.zeroturnaround.javarebel.integration.streambase;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import org.zeroturnaround.javarebel.LoggerFactory;

/**
 * @author Rein Raudjärv
 */
public class JarUtil {
  
  /** URL protocol for a file in the file system: "file" */
  public static final String URL_PROTOCOL_FILE = "file";

  /** URL protocol for an entry from a jar file: "jar" */
  public static final String URL_PROTOCOL_JAR = "jar";

  /** URL prefix for loading an entry from a jar file: "jar:file:" */
  public static final String JAR_FILE_URL_PREFIX = URL_PROTOCOL_JAR + ":" + URL_PROTOCOL_FILE + ":";

  /** Separator between JAR URL and file path within the JAR */
  public static final String JAR_URL_SEPARATOR = "!/";
  

  public static URL toURL(JarFile file, JarEntry entry) {
    if (file == null || entry == null)
      return null;
    
    StringBuffer sb = new StringBuffer();
    sb.append(JAR_FILE_URL_PREFIX);
    sb.append(file.getName());
    sb.append(JAR_URL_SEPARATOR);
    sb.append(entry.getName());
    String spec = sb.toString();
    
    try {
      return new URL(spec);
    } catch (MalformedURLException e) {
      LoggerFactory.getInstance().error(e);
      return null;
    }
  }
  
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy