1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * Marc-Andre Laperle - Initial API and implementation.
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.importtrace
;
16 import java
.io
.FileInputStream
;
17 import java
.io
.IOException
;
18 import java
.io
.InputStream
;
19 import java
.util
.Enumeration
;
20 import java
.util
.zip
.GZIPInputStream
;
23 * Wrapper for a Gzipped file
25 public class GzipFile
implements AutoCloseable
{
27 private static final String GZIP_EXTENSION
= ".gz"; //$NON-NLS-1$
29 private final File fFile
;
30 private final GzipEntry fEntry
;
31 private GzipEntry fCurEntry
;
32 private boolean fIsClosed
= false;
34 private final InputStream fInternalEntryStream
;
37 * Create a new GzipFile for the given file.
39 * @param source the source file
41 * File not found and such
43 public GzipFile(File source
) throws IOException
{
46 InputStream in
= new FileInputStream(source
);
48 // Check if it's a GZIPInputStream.
49 fInternalEntryStream
= new GZIPInputStream(in
);
50 } catch (IOException e
) {
54 String name
= source
.getName();
55 fEntry
= new GzipEntry(name
.substring(0, name
.lastIndexOf(GZIP_EXTENSION
)));
60 * Close the tar file input stream.
62 * @throws IOException if the file cannot be successfully closed
65 public void close() throws IOException
{
66 if (fInternalEntryStream
!= null && !fIsClosed
) {
67 fInternalEntryStream
.close();
74 * Create a new GzipFile for the given path name.
77 * the filename of the gzip file
79 * if the file cannot be opened
81 public GzipFile(String filename
) throws IOException
{
82 this(new File(filename
));
86 * Returns an enumeration cataloguing the tar archive.
88 * @return enumeration of all files in the archive
90 public Enumeration
<GzipEntry
> entries() {
91 return new Enumeration
<GzipEntry
>() {
93 public boolean hasMoreElements() {
94 return (fCurEntry
!= null);
98 public GzipEntry
nextElement() {
99 GzipEntry oldEntry
= fCurEntry
;
107 * Returns a new InputStream for the given file in the tar archive.
111 * @return an input stream for the given file
113 public InputStream
getInputStream(GzipEntry entry
) {
114 if (entry
!= fEntry
) {
115 throw new IllegalArgumentException();
117 return fInternalEntryStream
;
121 * Returns the path name of the file this archive represents.
125 public String
getName() {
126 return fFile
.getPath();
130 protected void finalize() throws Throwable
{