KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > wizards > datatransfer > TarFile


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.wizards.datatransfer;
12
13 import java.io.File JavaDoc;
14 import java.io.FileInputStream JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.InputStream JavaDoc;
17 import java.util.Enumeration JavaDoc;
18 import java.util.zip.GZIPInputStream JavaDoc;
19
20 /**
21  * Reads a .tar or .tar.gz archive file, providing an index enumeration
22  * and allows for accessing an InputStream for arbitrary files in the
23  * archive.
24  *
25  * @since 3.1
26  */

27 public class TarFile {
28     private File JavaDoc file;
29     private TarInputStream entryEnumerationStream;
30     private TarEntry curEntry;
31     private TarInputStream entryStream;
32     
33     /**
34      * Create a new TarFile for the given file.
35      *
36      * @param file
37      * @throws TarException
38      * @throws IOException
39      */

40     public TarFile(File JavaDoc file) throws TarException, IOException JavaDoc {
41         this.file = file;
42
43         InputStream JavaDoc in = new FileInputStream JavaDoc(file);
44         // First, check if it's a GZIPInputStream.
45
try {
46             in = new GZIPInputStream JavaDoc(in);
47         } catch(IOException JavaDoc e) {
48             //If it is not compressed we close
49
//the old one and recreate
50
in.close();
51             in = new FileInputStream JavaDoc(file);
52         }
53         entryEnumerationStream = new TarInputStream(in);
54         curEntry = entryEnumerationStream.getNextEntry();
55     }
56     
57     /**
58      * Close the tar file input stream.
59      *
60      * @throws IOException if the file cannot be successfully closed
61      */

62     public void close() throws IOException JavaDoc {
63         entryEnumerationStream.close();
64     }
65
66     /**
67      * Create a new TarFile for the given path name.
68      *
69      * @param filename
70      * @throws TarException
71      * @throws IOException
72      */

73     public TarFile(String JavaDoc filename) throws TarException, IOException JavaDoc {
74         this(new File JavaDoc(filename));
75     }
76
77     /**
78      * Returns an enumeration cataloguing the tar archive.
79      *
80      * @return enumeration of all files in the archive
81      */

82     public Enumeration JavaDoc entries() {
83         return new Enumeration JavaDoc() {
84             public boolean hasMoreElements() {
85                 return (curEntry != null);
86             }
87             
88             public Object JavaDoc nextElement() {
89                 TarEntry oldEntry = curEntry;
90                 try {
91                     curEntry = entryEnumerationStream.getNextEntry();
92                 } catch(TarException e) {
93                     curEntry = null;
94                 } catch(IOException JavaDoc e) {
95                     curEntry = null;
96                 }
97                 return oldEntry;
98             }
99         };
100     }
101
102     /**
103      * Returns a new InputStream for the given file in the tar archive.
104      *
105      * @param entry
106      * @return an input stream for the given file
107      * @throws TarException
108      * @throws IOException
109      */

110     public InputStream JavaDoc getInputStream(TarEntry entry) throws TarException, IOException JavaDoc {
111         if(entryStream == null || !entryStream.skipToEntry(entry)) {
112             InputStream JavaDoc in = new FileInputStream JavaDoc(file);
113             // First, check if it's a GZIPInputStream.
114
try {
115                 in = new GZIPInputStream JavaDoc(in);
116             } catch(IOException JavaDoc e) {
117                 in = new FileInputStream JavaDoc(file);
118             }
119             entryStream = new TarInputStream(in, entry) {
120                 public void close() {
121                     // Ignore close() since we want to reuse the stream.
122
}
123             };
124         }
125         if(entryStream == null) {
126             System.out.println("huh?"); //$NON-NLS-1$
127
}
128         return entryStream;
129     }
130
131     /**
132      * Returns the path name of the file this archive represents.
133      *
134      * @return path
135      */

136     public String JavaDoc getName() {
137         return file.getPath();
138     }
139 }
140
Popular Tags