• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

devonfw / IDEasy / 9907372175

12 Jul 2024 11:49AM UTC coverage: 61.142% (-0.02%) from 61.162%
9907372175

push

github

hohwille
fixed tests

1997 of 3595 branches covered (55.55%)

Branch coverage included in aggregate %.

5296 of 8333 relevant lines covered (63.55%)

2.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

80.26
cli/src/main/java/com/devonfw/tools/ide/io/TarCompression.java
1
package com.devonfw.tools.ide.io;
2

3
import java.io.IOException;
4
import java.io.InputStream;
5
import java.util.Locale;
6

7
import org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream;
8
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
9

10
/**
11
 * {@link Enum} with the available compression modes of a TAR archive file. A GNU Tape ARchive is the standard archive format on Linux systems. It is similar to
12
 * ZIP but it allows to represent advanced metadata such as file permissions (e.g. executable flags). Further, it has no compression and is therefore typically
13
 * combined with generic file compressions like {@link #GZ GNU zip} (not to be confused with Windows ZIP) or {@link #BZIP2}.
14
 */
15
public enum TarCompression {
3✔
16

17
  /** No compression (uncompressed TAR). */
18
  NONE("", "tar", null),
9✔
19

20
  /** GNU-Zip compression. */
21
  GZ("gz", "tgz", "-z") {
17✔
22
    @Override
23
    InputStream unpackRaw(InputStream in) throws IOException {
24

25
      return new GzipCompressorInputStream(in);
5✔
26
    }
27
  },
28

29
  /** BZip2 compression. */
30
  BZIP2("bz2", "tbz2", "-j", "bzip2") {
19✔
31
    @Override
32
    InputStream unpackRaw(InputStream in) throws IOException {
33

34
      return new BZip2CompressorInputStream(in);
5✔
35
    }
36
  };
37

38
  private final String extension;
39

40
  private final String combinedExtension;
41

42
  private final String tarOption;
43

44
  private final String altExtension;
45

46
  private TarCompression(String extension, String combinedExtension, String tarOption) {
47

48
    this(extension, combinedExtension, tarOption, null);
8✔
49
  }
1✔
50

51
  private TarCompression(String extension, String combinedExtension, String tarOption, String altExtension) {
4✔
52

53
    this.extension = extension;
3✔
54
    this.combinedExtension = combinedExtension;
3✔
55
    this.tarOption = tarOption;
3✔
56
    this.altExtension = altExtension;
3✔
57
  }
1✔
58

59
  /**
60
   * @return the (default) file extension of this compression (excluding the dot). E.g. "gz" for a "tar.gz" or "tgz" file.
61
   */
62
  public String getExtension() {
63

64
    return this.extension;
×
65
  }
66

67
  /**
68
   * @return the compact file extension of this compression combined with the tar archive information. E.g. "tgz" or "tbz2".
69
   */
70
  public String getCombinedExtension() {
71

72
    return this.combinedExtension;
×
73
  }
74

75
  /**
76
   * @return the CLI option to enable this compression in the GNU tar command.
77
   */
78
  public String getTarOption() {
79

80
    return this.tarOption;
×
81
  }
82

83
  /**
84
   * @return altExtension
85
   */
86
  public String getAltExtension() {
87

88
    return this.altExtension;
×
89
  }
90

91
  /**
92
   * @param in the {@link InputStream} to wrap for unpacking.
93
   * @return an {@link InputStream} to read the unpacked payload of the given {@link InputStream}.
94
   */
95
  public final InputStream unpack(InputStream in) {
96

97
    try {
98
      return unpackRaw(in);
4✔
99
    } catch (IOException e) {
×
100
      throw new IllegalStateException("Failed to open unpacking stream!", e);
×
101
    }
102
  }
103

104
  InputStream unpackRaw(InputStream in) throws IOException {
105

106
    return in;
2✔
107
  }
108

109
  /**
110
   * @param filename the filename or extension (e.g. "archive.tar.bzip2", "tgz", ".tar.gz", etc.)
111
   * @return the {@link TarCompression} detected from the given {@code filename} or {@code null} if none was detected.
112
   */
113
  public static TarCompression of(String filename) {
114

115
    if ((filename == null) || filename.isEmpty()) {
5!
116
      return null;
×
117
    }
118
    String ext = filename.toLowerCase(Locale.ROOT);
4✔
119
    int tarIndex = ext.lastIndexOf("tar");
4✔
120
    if (tarIndex >= 0) {
2✔
121
      if ((tarIndex == 0) || (ext.charAt(tarIndex - 1) == '.')) {
9!
122
        int tarEnd = tarIndex + 3;
4✔
123
        int rest = ext.length() - tarEnd;
5✔
124
        if (rest == 0) {
2✔
125
          return NONE;
2✔
126
        }
127
        if (ext.charAt(tarEnd) == '.') {
5!
128
          String compression = ext.substring(tarEnd + 1);
6✔
129
          for (TarCompression cmp : values()) {
16✔
130
            if (compression.equals(cmp.extension) || compression.equals(cmp.altExtension)) {
10✔
131
              return cmp;
2✔
132
            }
133
          }
134
        }
135
      }
136
      return null;
2✔
137
    }
138
    int lastDot = ext.lastIndexOf('.');
4✔
139
    if (lastDot > 0) {
2✔
140
      ext = ext.substring(lastDot + 1);
6✔
141
    }
142
    for (TarCompression cmp : values()) {
16✔
143
      if (ext.equals(cmp.combinedExtension) || (ext.endsWith(cmp.combinedExtension)
10!
144
          && ext.charAt(ext.length() - cmp.combinedExtension.length() - 1) == '.')) {
×
145
        return cmp;
2✔
146
      }
147
    }
148
    return null;
2✔
149
  }
150

151
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc