3568 |
18 Jul 07 |
martin |
1 |
/** |
3568 |
18 Jul 07 |
martin |
$Id$ |
3568 |
18 Jul 07 |
martin |
3 |
|
3675 |
16 Aug 07 |
jari |
Copyright (C) 2007 Nicklas Nordborg, Martin Svensson |
3568 |
18 Jul 07 |
martin |
5 |
|
3568 |
18 Jul 07 |
martin |
This file is part of BASE - BioArray Software Environment. |
3568 |
18 Jul 07 |
martin |
Available at http://base.thep.lu.se/ |
3568 |
18 Jul 07 |
martin |
8 |
|
3568 |
18 Jul 07 |
martin |
BASE is free software; you can redistribute it and/or modify it |
3568 |
18 Jul 07 |
martin |
under the terms of the GNU General Public License as published by |
4479 |
05 Sep 08 |
jari |
the Free Software Foundation; either version 3 of the License, or |
3568 |
18 Jul 07 |
martin |
(at your option) any later version. |
3568 |
18 Jul 07 |
martin |
13 |
|
3568 |
18 Jul 07 |
martin |
BASE is distributed in the hope that it will be useful, but |
3568 |
18 Jul 07 |
martin |
WITHOUT ANY WARRANTY; without even the implied warranty of |
3568 |
18 Jul 07 |
martin |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3568 |
18 Jul 07 |
martin |
General Public License for more details. |
3568 |
18 Jul 07 |
martin |
18 |
|
3568 |
18 Jul 07 |
martin |
You should have received a copy of the GNU General Public License |
4515 |
11 Sep 08 |
jari |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
3568 |
18 Jul 07 |
martin |
21 |
*/ |
3568 |
18 Jul 07 |
martin |
22 |
|
3641 |
08 Aug 07 |
nicklas |
23 |
package net.sf.basedb.util.zip; |
3568 |
18 Jul 07 |
martin |
24 |
|
3568 |
18 Jul 07 |
martin |
25 |
import net.sf.basedb.util.FileUtil; |
3568 |
18 Jul 07 |
martin |
26 |
|
3568 |
18 Jul 07 |
martin |
27 |
import java.io.IOException; |
3641 |
08 Aug 07 |
nicklas |
28 |
import java.io.InputStream; |
3568 |
18 Jul 07 |
martin |
29 |
import java.io.OutputStream; |
3568 |
18 Jul 07 |
martin |
30 |
|
3568 |
18 Jul 07 |
martin |
31 |
import com.ice.tar.TarEntry; |
3568 |
18 Jul 07 |
martin |
32 |
import com.ice.tar.TarOutputStream; |
3568 |
18 Jul 07 |
martin |
33 |
|
3568 |
18 Jul 07 |
martin |
34 |
/** |
3641 |
08 Aug 07 |
nicklas |
This class packs files into a TAR archive. |
3641 |
08 Aug 07 |
nicklas |
@author martin, Nicklas |
3568 |
18 Jul 07 |
martin |
@version 2.4 |
3568 |
18 Jul 07 |
martin |
@base.modified $Date: 2007-07-11 15:45:01 +0200 (Mon, 02 Jul 2007) $ |
3641 |
08 Aug 07 |
nicklas |
39 |
*/ |
3568 |
18 Jul 07 |
martin |
40 |
public class TarFilePacker |
3568 |
18 Jul 07 |
martin |
41 |
implements FilePacker |
3568 |
18 Jul 07 |
martin |
42 |
{ |
3568 |
18 Jul 07 |
martin |
43 |
|
3641 |
08 Aug 07 |
nicklas |
44 |
private TarOutputStream tar; |
3568 |
18 Jul 07 |
martin |
45 |
|
3568 |
18 Jul 07 |
martin |
46 |
/** |
3568 |
18 Jul 07 |
martin |
Creates a new TarFilePacker. |
3568 |
18 Jul 07 |
martin |
48 |
*/ |
3641 |
08 Aug 07 |
nicklas |
49 |
public TarFilePacker() |
3641 |
08 Aug 07 |
nicklas |
50 |
{} |
3568 |
18 Jul 07 |
martin |
51 |
|
3568 |
18 Jul 07 |
martin |
52 |
/* |
3641 |
08 Aug 07 |
nicklas |
From the FilePacker interface |
3641 |
08 Aug 07 |
nicklas |
54 |
------------------------------------------- |
3568 |
18 Jul 07 |
martin |
55 |
*/ |
3641 |
08 Aug 07 |
nicklas |
56 |
/** |
3643 |
08 Aug 07 |
nicklas |
@return "TAR-archive (.tar)" |
3641 |
08 Aug 07 |
nicklas |
58 |
*/ |
6127 |
14 Sep 12 |
nicklas |
59 |
@Override |
3641 |
08 Aug 07 |
nicklas |
60 |
public String getDescription() |
3568 |
18 Jul 07 |
martin |
61 |
{ |
3643 |
08 Aug 07 |
nicklas |
62 |
return "TAR-archive (.tar)"; |
3568 |
18 Jul 07 |
martin |
63 |
} |
3641 |
08 Aug 07 |
nicklas |
64 |
/** |
3641 |
08 Aug 07 |
nicklas |
@return "tar" |
3568 |
18 Jul 07 |
martin |
66 |
*/ |
6127 |
14 Sep 12 |
nicklas |
67 |
@Override |
3641 |
08 Aug 07 |
nicklas |
68 |
public String getFileExtension() |
3568 |
18 Jul 07 |
martin |
69 |
{ |
3641 |
08 Aug 07 |
nicklas |
70 |
return "tar"; |
3568 |
18 Jul 07 |
martin |
71 |
} |
3641 |
08 Aug 07 |
nicklas |
72 |
/** |
3641 |
08 Aug 07 |
nicklas |
@return "application/x-tar" |
3568 |
18 Jul 07 |
martin |
74 |
*/ |
6127 |
14 Sep 12 |
nicklas |
75 |
@Override |
3641 |
08 Aug 07 |
nicklas |
76 |
public String getMimeType() |
3568 |
18 Jul 07 |
martin |
77 |
{ |
3641 |
08 Aug 07 |
nicklas |
78 |
return "application/x-tar"; |
3568 |
18 Jul 07 |
martin |
79 |
} |
3641 |
08 Aug 07 |
nicklas |
80 |
/** |
3641 |
08 Aug 07 |
nicklas |
* Wrap the output stream in a {@link TarOutputStream}. |
3641 |
08 Aug 07 |
nicklas |
82 |
*/ |
6127 |
14 Sep 12 |
nicklas |
83 |
@Override |
3641 |
08 Aug 07 |
nicklas |
84 |
public void setOutputStream(OutputStream out) |
3641 |
08 Aug 07 |
nicklas |
85 |
throws IOException |
3568 |
18 Jul 07 |
martin |
86 |
{ |
3641 |
08 Aug 07 |
nicklas |
87 |
this.tar = new TarOutputStream(out); |
3568 |
18 Jul 07 |
martin |
88 |
} |
3568 |
18 Jul 07 |
martin |
89 |
/** |
3641 |
08 Aug 07 |
nicklas |
Create a new {@link TarEntry} and write the compressed |
3641 |
08 Aug 07 |
nicklas |
data to it. |
3568 |
18 Jul 07 |
martin |
92 |
*/ |
6127 |
14 Sep 12 |
nicklas |
93 |
@Override |
3641 |
08 Aug 07 |
nicklas |
94 |
public void pack(String entryName, InputStream in, long size, long lastModified) |
3641 |
08 Aug 07 |
nicklas |
95 |
throws IOException |
3568 |
18 Jul 07 |
martin |
96 |
{ |
3641 |
08 Aug 07 |
nicklas |
97 |
boolean isDirectory = in == null; |
3641 |
08 Aug 07 |
nicklas |
98 |
if (isDirectory && !entryName.endsWith("/")) entryName += "/"; |
3641 |
08 Aug 07 |
nicklas |
99 |
TarEntry entry = new TarEntry(entryName); |
3641 |
08 Aug 07 |
nicklas |
100 |
if (lastModified > 0) entry.setModTime(lastModified); |
3641 |
08 Aug 07 |
nicklas |
101 |
entry.setSize(size); |
4095 |
21 Jan 08 |
enell |
102 |
TarUtil.putNextEntry(entry, tar); |
3641 |
08 Aug 07 |
nicklas |
103 |
if (!isDirectory) FileUtil.copy(in, tar); |
3641 |
08 Aug 07 |
nicklas |
104 |
tar.closeEntry(); |
3568 |
18 Jul 07 |
martin |
105 |
} |
3568 |
18 Jul 07 |
martin |
106 |
/** |
3641 |
08 Aug 07 |
nicklas |
Finish the TAR file. |
3568 |
18 Jul 07 |
martin |
108 |
*/ |
6127 |
14 Sep 12 |
nicklas |
109 |
@Override |
3641 |
08 Aug 07 |
nicklas |
110 |
public void close() |
3641 |
08 Aug 07 |
nicklas |
111 |
throws IOException |
3568 |
18 Jul 07 |
martin |
112 |
{ |
3646 |
09 Aug 07 |
nicklas |
113 |
tar.close(); |
3641 |
08 Aug 07 |
nicklas |
114 |
tar = null; |
3568 |
18 Jul 07 |
martin |
115 |
} |
3641 |
08 Aug 07 |
nicklas |
116 |
// ------------------------------------------- |
3568 |
18 Jul 07 |
martin |
117 |
|
3568 |
18 Jul 07 |
martin |
118 |
} |