Zip URLConnection : URLConnection « Network Protocol « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Network Protocol » URLConnection 




Zip URLConnection
     
/**
 * The utillib library.
 * More information is available at http://www.jinchess.com/.
 * Copyright (C) 2003 Alexander Maryanovsky.
 * All rights reserved.
 *
 * The utillib library is free software; you can redistribute
 * it and/or modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * The utillib library is distributed in the hope that it will
 * be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser
 * General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with utillib library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */



import java.net.URL;
import java.net.URLConnection;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipFile;
import java.util.zip.ZipEntry;


/**
 * A <code>URLConnection</code> which loads its content from a specified zip
 * entry within a local file.
 */

public class ZipURLConnection extends URLConnection{



  /**
   * The zip file.
   */

  private final File file;



  /**
   * The name of the zip entry within the zip file.
   */

  private final String zipEntryName;



  /**
   * The <code>ZipFile</code> object for the zip file. Created when
   * <code>connect()</code> is called.
   */

  private ZipFile zipFile = null;



  /**
   * The <code>ZipEntry</code> object for the entry in the zip file. Created
   * when <code>connect()</code> is called.
   */

  private ZipEntry zipEntry = null;



  /**
   * Creates a new <code>ZipURLConnection</code> for the specified zip entry
   * within the specified <code>File</code>.
   */

  public ZipURLConnection(URL url, File file, String zipEntryName){
    super(url);

    this.file = file;
    this.zipEntryName = zipEntryName;
  }



  /**
   * Attempts to open the zip entry.
   */

  public void connect() throws IOException{
    this.zipFile = new ZipFile(file);
    this.zipEntry = zipFile.getEntry(zipEntryName);
    if (zipEntry == null)
      throw new IOException("Entry " + zipEntryName + " not found in file " + file);
    this.connected = true;
  }



  /**
   * Returns the <code>InputStream</code> that reads from this connection.
   */

  public InputStream getInputStream() throws IOException{
    if (!connected)
      connect();

    return zipFile.getInputStream(zipEntry);
  }



  /**
   * Returns the length of the uncompressed zip entry.
   */

  public int getContentLength(){
    if (!connected)
      return -1;

    return (int)zipEntry.getSize();
  }


}

   
    
    
    
    
  














Related examples in the same category
1.Demonstrate URLConnection.
2.Call a servlet from a Java command line application
3.A CGI POST Example
4.Http authentication header
5.URLConnection.setRequestProperty
6.File size from URL
7.Sending a POST Request Using a URL
8.Check if a file was modified on the server
9.Getting Text from a URL
10.Getting an Image from a URL
11.Sending a POST Request with Parameters From a Java Class
12.Downloading a web page using URL and URLConnection classes
13.Get response header from HTTP request
14.Read / download webpage content
15.Read a GIF or CLASS from an URL save it locally
16.Zip URLStream Handler
17.Http Parser
18.Http Constants
19.Get URLConnection Expiration
20.Locating files by path or URL
21.Download from URL
22.This program demonstrates how to use the URLConnection class for a POST request.
23.Connects to an URL and displays the response header data and the first 10 lines of the requested data.Connects to an URL and displays the response header data and the first 10 lines of the requested data.
24.Load content from URL to string
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.