summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/openslx/util/TarArchiveUtil.java
blob: 9970b0b72630a217af633c24d05250e96fcf192e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package org.openslx.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;

public class TarArchiveUtil
{

	private TarArchiveUtil()
	{
	}

	public static class TarArchiveReader implements AutoCloseable
	{
		private final TarArchiveInputStream tarInputStream;
		private TarArchiveEntry currentEntry = null;

		public TarArchiveReader( InputStream in ) throws IOException
		{
			this( in, true, false );
		}

		public TarArchiveReader( InputStream in, boolean isBuffered, boolean isCompressed ) throws IOException
		{
			InputStream stream = in;
			if ( isBuffered ) {
				stream = new BufferedInputStream( stream );
			}

			if ( isCompressed ) {
				stream = new GZIPInputStream( stream );
			}

			this.tarInputStream = new TarArchiveInputStream( stream );
		}

		public boolean hasNextEntry() throws IOException
		{
			this.currentEntry = this.tarInputStream.getNextTarEntry();
			if ( this.currentEntry != null ) {
				return true;
			} else {
				return false;
			}
		}

		public String getEntryName()
		{
			if ( this.currentEntry == null )
				return null;

			return this.currentEntry.getName();
		}

		public byte[] readCurrentEntry() throws IOException
		{
			ByteArrayOutputStream output = new ByteArrayOutputStream();
			byte[] rawData = new byte[ 1024 ];
			int count = 0;

			while ( ( count = this.tarInputStream.read( rawData ) ) != -1 ) {
				output.write( rawData, 0, count );
			}

			return output.toByteArray();
		}

		@Override
		public void close() throws IOException
		{
			tarInputStream.close();
		}

	}

	public static class TarArchiveWriter implements AutoCloseable
	{
		private final TarArchiveOutputStream tarOutputStream;

		public TarArchiveWriter( OutputStream out ) throws IOException
		{
			this( out, true, true );
		}

		public TarArchiveWriter( OutputStream out, boolean isBuffered, boolean isCompressed ) throws IOException
		{
			OutputStream stream = out;

			if ( isBuffered ) {
				stream = new BufferedOutputStream( stream );
			}

			if ( isCompressed ) {
				stream = new GZIPOutputStream( stream );
			}

			this.tarOutputStream = new TarArchiveOutputStream( stream );
		}

		public void writeFile( String filename, String data ) throws IOException
		{

			if ( data == null )
				return;
			putFile( filename, data.getBytes( StandardCharsets.UTF_8 ) );
		}

		public void writeFile( String filename, byte[] data ) throws IOException
		{
			if ( data == null )
				return;
			putFile( filename, data );
		}

		private void putFile( String filename, byte[] data ) throws IOException
		{
			if ( data == null )
				return;

			TarArchiveEntry entry = new TarArchiveEntry( filename );
			entry.setSize( data.length );
			entry.setModTime( System.currentTimeMillis() );
			entry.setMode( 0644 );
			tarOutputStream.putArchiveEntry( entry );
			tarOutputStream.write( data );
			tarOutputStream.closeArchiveEntry();
		}

		@Override
		public void close() throws IOException
		{
			tarOutputStream.close();
		}
	}
}