summaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble/mammoth/internal/archives/ZippedArchive.java
blob: 47be074c060d46e9d54701eadcd9cf632fcef1c3 (plain)
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
package org.zwobble.mammoth.internal.archives;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Optional;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class ZippedArchive implements Archive {
    private final ZipFile zipFile;

    public ZippedArchive(ZipFile zipFile) {
        this.zipFile = zipFile;
    }

    public ZippedArchive(File file) throws IOException {
        this(new ZipFile(file));
    }

    @Override
    public Optional<InputStream> tryGetInputStream(String name) throws IOException {
        ZipEntry entry = zipFile.getEntry(name);
        if (entry == null) {
            return Optional.empty();
        } else {
            return Optional.of(zipFile.getInputStream(entry));
        }
    }

    @Override
    public boolean exists(String name) {
        return zipFile.getEntry(name) != null;
    }

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