Java URL Connection isZipName(URI documentIRI, URLConnection connection)

Here you can find the source of isZipName(URI documentIRI, URLConnection connection)

Description

is Zip Name

License

Open Source License

Declaration

private static boolean isZipName(URI documentIRI, URLConnection connection) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.net.URI;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    private static final String ZIP_FILE_EXTENSION = ".zip";
    private static final String CONTENT_DISPOSITION_HEADER = "Content-Disposition";
    private static final Pattern CONTENT_DISPOSITION_FILE_NAME_PATTERN = Pattern
            .compile(".*filename=\"([^\\s;]*)\".*");
    private static final int CONTENT_DISPOSITION_FILE_NAME_PATTERN_GROUP = 1;

    private static boolean isZipName(URI documentIRI, URLConnection connection) {
        if (isZipFileName(documentIRI.toString())) {
            return true;
        } else {/* w w w . j  a va  2  s  . com*/
            String fileName = getFileNameFromContentDisposition(connection);
            return fileName != null && isZipFileName(fileName);
        }
    }

    private static boolean isZipFileName(String fileName) {
        return fileName.toLowerCase().endsWith(ZIP_FILE_EXTENSION);
    }

    private static String getFileNameFromContentDisposition(URLConnection connection) {
        String contentDispositionHeaderValue = connection.getHeaderField(CONTENT_DISPOSITION_HEADER);
        if (contentDispositionHeaderValue != null) {
            Matcher matcher = CONTENT_DISPOSITION_FILE_NAME_PATTERN.matcher(contentDispositionHeaderValue);
            if (matcher.matches()) {
                return matcher.group(CONTENT_DISPOSITION_FILE_NAME_PATTERN_GROUP);
            }
        }
        return null;
    }
}

Related

  1. isJarDirectory(JarURLConnection conn)
  2. isOnline(String url)
  3. isUrlReachable(String url)
  4. isValidToc(URL url)
  5. isValidUrl(String input)
  6. jarURLDirectories(URL jarURL)
  7. jarURLEntryResource(String jarURLString)
  8. lastModifiedURL(String urlstr)
  9. loadCookie(URL url)