1 package org
.argeo
.slc
.deb
;
3 import java
.io
.BufferedInputStream
;
4 import java
.io
.IOException
;
5 import java
.io
.UncheckedIOException
;
7 import java
.nio
.charset
.StandardCharsets
;
8 import java
.util
.function
.Consumer
;
10 import org
.apache
.commons
.compress
.archivers
.ArchiveEntry
;
11 import org
.apache
.commons
.compress
.archivers
.ArchiveInputStream
;
12 import org
.apache
.commons
.compress
.archivers
.ar
.ArArchiveEntry
;
13 import org
.apache
.commons
.compress
.archivers
.ar
.ArArchiveInputStream
;
14 import org
.apache
.commons
.compress
.archivers
.tar
.TarArchiveInputStream
;
15 import org
.apache
.commons
.compress
.compressors
.CompressorException
;
16 import org
.apache
.commons
.compress
.compressors
.CompressorStreamFactory
;
18 public class DebFile
{
21 public DebFile(URL url
) {
25 public void printDataArchive() {
26 openDataArchive((tarIn
) -> {
28 ArchiveEntry entry
= null;
29 while ((entry
= tarIn
.getNextEntry()) != null) {
30 System
.out
.println(entry
.getName());
32 } catch (IOException e
) {
33 throw new UncheckedIOException(e
);
38 public void printControlArchive() {
39 openControlArchive((tarIn
) -> {
41 ArchiveEntry entry
= null;
42 while ((entry
= tarIn
.getNextEntry()) != null) {
43 System
.out
.println(entry
.getName());
45 } catch (IOException e
) {
46 throw new UncheckedIOException(e
);
51 public void printControlFile() {
52 openControlArchive((tarIn
) -> {
54 ArchiveEntry entry
= null;
55 while ((entry
= tarIn
.getNextEntry()) != null) {
56 if ("./control".equals(entry
.getName())) {
57 String controlFileStr
= new String(tarIn
.readAllBytes(), StandardCharsets
.UTF_8
);
58 System
.out
.println(controlFileStr
);
61 } catch (IOException e
) {
62 throw new UncheckedIOException(e
);
67 protected void openDataArchive(Consumer
<ArchiveInputStream
> process
) {
68 try (ArArchiveInputStream arIn
= new ArArchiveInputStream(new BufferedInputStream(url
.openStream()))) {
69 // order is specified, so we don't verify entry names
70 ArArchiveEntry debianBinaryEntry
= arIn
.getNextArEntry();
71 assert "debian-binary".equals(debianBinaryEntry
.getName());
73 arIn
.getNextArEntry();// control archive
74 ArArchiveEntry dataArchiveEntry
= arIn
.getNextArEntry();
75 processArArchiveEntry(dataArchiveEntry
, arIn
, process
);
76 } catch (IOException
| CompressorException e
) {
77 throw new RuntimeException("Cannot open data archive of " + url
, e
);
81 protected void openControlArchive(Consumer
<ArchiveInputStream
> process
) {
82 try (ArArchiveInputStream arIn
= new ArArchiveInputStream(new BufferedInputStream(url
.openStream()))) {
83 // order is specified, so we don't verify entry names
84 ArArchiveEntry debianBinaryEntry
= arIn
.getNextArEntry();
85 assert "debian-binary".equals(debianBinaryEntry
.getName());
87 ArArchiveEntry controlArchiveEntry
= arIn
.getNextArEntry();
88 processArArchiveEntry(controlArchiveEntry
, arIn
, process
);
89 } catch (IOException
| CompressorException e
) {
90 throw new RuntimeException("Cannot open control archive of " + url
, e
);
94 protected void processArArchiveEntry(ArchiveEntry archiveEntry
, ArArchiveInputStream arIn
,
95 Consumer
<ArchiveInputStream
> process
) throws IOException
, CompressorException
{
96 String dataArchiveName
= archiveEntry
.getName();
97 String compressionType
= null;
98 if (dataArchiveName
.endsWith(".xz"))
99 compressionType
= CompressorStreamFactory
.XZ
;
100 else if (dataArchiveName
.endsWith(".gz"))
101 compressionType
= CompressorStreamFactory
.GZIP
;
102 else if (dataArchiveName
.endsWith(".bzip2"))
103 compressionType
= CompressorStreamFactory
.BZIP2
;
104 else if (dataArchiveName
.endsWith(".lzma"))
105 compressionType
= CompressorStreamFactory
.LZMA
;
107 if (compressionType
== null) {
108 try (TarArchiveInputStream tarIn
= new TarArchiveInputStream(arIn
)) {
109 process
.accept(tarIn
);
112 try (TarArchiveInputStream tarIn
= new TarArchiveInputStream(
113 new CompressorStreamFactory().createCompressorInputStream(compressionType
, arIn
))) {
114 process
.accept(tarIn
);
119 protected void checkVersion(ArArchiveInputStream arIn
) throws IOException
{
120 String version
= new String(arIn
.readAllBytes(), StandardCharsets
.US_ASCII
).trim();
121 if (!"2.0".equals(version
))
122 throw new IllegalStateException("Deb " + version
+ " is not supported");
125 public static void main(String
[] args
) throws Exception
{
127 // .get(System.getProperty("user.home")+"/qa/git/argeo-qa-unstable/build/repo/deb/platform/2.3/argeo-cms_2.3.18_all.deb")
129 URL url
= new URL("http://repo.netiket.eu/deb/pool/bullseye/platform/2.3/argeo-cms_2.3.18_all.deb");
130 DebFile debFile
= new DebFile(url
);
131 debFile
.printControlArchive();
132 debFile
.printControlFile();
133 debFile
.printDataArchive();