Skip to content

Commit e006b40

Browse files
committed
Integrata aggiunta "Extracting InputStream directly" di daniel-rabe
edmund-wagner/junrar#34 I did some changes to JunRar to extract InputStreams directly, without the need to write it in a File first. My project loads files from websides (e.g. RAR-Files), the results are InputStreams of that files. When I want to know what is inside a RAR-File I don't want to write that InputStream in a File everytime, so I made this little changes to junrar. greets Daniel Rabe
1 parent 8709dcd commit e006b40

File tree

3 files changed

+109
-0
lines changed

3 files changed

+109
-0
lines changed

src/main/java/com/github/junrar/Archive.java

+10
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import com.github.junrar.exception.RarException;
3434
import com.github.junrar.exception.RarException.RarExceptionType;
3535
import com.github.junrar.impl.FileVolumeManager;
36+
import com.github.junrar.impl.InputStreamVolumeManager;
3637
import com.github.junrar.io.IReadOnlyAccess;
3738
import com.github.junrar.rarfile.AVHeader;
3839
import com.github.junrar.rarfile.BaseBlock;
@@ -118,6 +119,15 @@ public Archive(File firstVolume, UnrarCallback unrarCallback)
118119
this(new FileVolumeManager(firstVolume), unrarCallback);
119120
}
120121

122+
public Archive(InputStream firstVolume) throws RarException, IOException {
123+
this(new InputStreamVolumeManager(firstVolume), null);
124+
}
125+
126+
public Archive(InputStream firstVolume, UnrarCallback unrarCallback)
127+
throws RarException, IOException {
128+
this(new InputStreamVolumeManager(firstVolume), unrarCallback);
129+
}
130+
121131
// public File getFile() {
122132
// return file;
123133
// }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.github.junrar.impl;
2+
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.File;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
8+
import com.github.junrar.Archive;
9+
import com.github.junrar.Volume;
10+
import com.github.junrar.io.IReadOnlyAccess;
11+
import com.github.junrar.io.ReadOnlyAccessByteArray;
12+
13+
14+
/**
15+
* @author Daniel Rabe</a>
16+
*
17+
*/
18+
public class InputStreamVolume implements Volume {
19+
private final Archive archive;
20+
private final byte[] file;
21+
22+
/**
23+
* @param file
24+
* @throws IOException
25+
*/
26+
public InputStreamVolume(Archive archive, InputStream inputstream) throws IOException {
27+
this.archive = archive;
28+
byte[] buff = new byte[8000];
29+
int bytesRead = 0;
30+
ByteArrayOutputStream bao = new ByteArrayOutputStream();
31+
while((bytesRead = inputstream.read(buff)) != -1) {
32+
bao.write(buff, 0, bytesRead);
33+
}
34+
byte[] data = bao.toByteArray();
35+
this.file = data;
36+
}
37+
38+
@Override
39+
public IReadOnlyAccess getReadOnlyAccess() throws IOException {
40+
return new ReadOnlyAccessByteArray(file);
41+
}
42+
43+
@Override
44+
public long getLength() {
45+
return file.length;
46+
}
47+
48+
@Override
49+
public Archive getArchive() {
50+
return archive;
51+
}
52+
53+
/**
54+
* @return the file
55+
*/
56+
public File getFile() {
57+
// there is no File object anymore
58+
return null;
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.github.junrar.impl;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
7+
import com.github.junrar.Archive;
8+
import com.github.junrar.Volume;
9+
import com.github.junrar.VolumeManager;
10+
import com.github.junrar.util.VolumeHelper;
11+
12+
13+
/**
14+
* @author Daniel Rabe</a>
15+
*
16+
*/
17+
public class InputStreamVolumeManager implements VolumeManager {
18+
private final InputStream firstVolume;
19+
20+
public InputStreamVolumeManager(InputStream firstVolume) {
21+
this.firstVolume = firstVolume;
22+
}
23+
24+
@Override
25+
public Volume nextArchive(Archive archive, Volume last)
26+
throws IOException {
27+
if (last == null)
28+
return new InputStreamVolume(archive, firstVolume);
29+
30+
FileVolume lastFileVolume = (FileVolume) last;
31+
boolean oldNumbering = !archive.getMainHeader().isNewNumbering()
32+
|| archive.isOldFormat();
33+
String nextName = VolumeHelper.nextVolumeName(lastFileVolume.getFile()
34+
.getAbsolutePath(), oldNumbering);
35+
File nextVolume = new File(nextName);
36+
37+
return new FileVolume(archive, nextVolume);
38+
}
39+
}

0 commit comments

Comments
 (0)