blib/lib/Archive/SCS/Zip.pm untested

filter: Statementstmt 0.0% 0 / 116 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 53 MC/DCmcdc 0.0% 0 / 29 Subroutinesub 0.0% 0 / 11 Podpod n/a totaltotal 0.0% 0 / 221 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 use v5.34;
2 use warnings;
3 use Object::Pad 0.73;
4  
5 class Archive::SCS::Zip 1.12
6 :isa( Archive::SCS::Mountable );
7  
8 use builtin::compat qw( blessed true );
9 use stable 0.031 'isa';
10  
11 use Archive::SCS::CityHash 'cityhash64';
12 use Archive::SCS::DirIndex;
13 use Carp 'croak';
14 use IO::Compress::Zip qw( :constants :zip_method $ZipError );
15 use IO::Uncompress::Unzip qw( $UnzipError );
16 use Path::Tiny 0.054 ();
17  
18 our @CARP_NOT = qw( Archive::SCS );
19  
20 my $LOCAL_HEADER_SIGNATURE = "PK\x03\x04";
21  
22 field $path :param :reader;
23 field $zip;
24  
25 field %entries;
26 field @dirs;
27 field @files;
28  
29 ADJUST {
30 $path isa Path::Tiny && $path->exists or croak
31 "Param file must be a valid Path::Tiny object";
32 }
33  
34  
35 method handles_path :common ($path, $header) {
36 $header =~ /^\Q$LOCAL_HEADER_SIGNATURE\E/
37 # According to the "appnote" spec, ZIP files don't begin with magic. Instead,
38 # we'd need to scan the file's last 66 KB for the "end of central directory
39 # record" signature, then heuristically determine if it looks like a ZIP file.
40 # However, in practice, the overwhelming majority of ZIP files starts with a
41 # "local file header" signature, which we can simply check for here.
42 }
43  
44  
45 method mount () {
46 my $filename = $path->basename;
47 $self->is_mounted and croak sprintf "%s: Already mounted", $filename;
48  
49 $zip = IO::Uncompress::Unzip->new( "$path", Append => 1 ) or croak
50 "$filename: IO::Uncompress init: $UnzipError";
51  
52 my $status = 1;
53 while ($status > 0) {
54 my $entry = $zip->getHeaderInfo;
55  
56 my $name = $entry->{Name};
57 my $is_empty = blessed $entry->{UncompressedLength} # type seems to vary by version
58 ? $entry->{UncompressedLength}->isZero
59 : $entry->{UncompressedLength} == 0;
60  
61 $entry->{is_dir} = $is_empty && $name =~ s|/\z||;
62 $entries{ cityhash64 $name } = $entry;
63  
64 if ($entry->{is_dir}) {
65 push @dirs, $name;
66 next;
67 }
68 push @files, $name;
69  
70 $entry->{__data} = '';
71 $status = $zip->read($entry->{__data}) while $status > 0;
72 $status < 0 and croak
73 "$filename: IO::Uncompress read ($entry->{Name}): $UnzipError ($status $!)";
74 }
75 continue {
76 $status = $zip->nextStream;
77 $status < 0 and croak
78 "$filename: IO::Uncompress nextStream: $UnzipError ($!)";
79 }
80 $zip->close;
81  
82 return $self;
83 }
84  
85  
86 method unmount () {
87 undef $zip;
88 undef %entries;
89 undef @dirs;
90 undef @files;
91 }
92  
93  
94 method is_mounted () {
95 !! $zip
96 }
97  
98  
99 method read_dir_tree (@roots) {
100 my %index = Archive::SCS::DirIndex->auto_index(\@files, \@dirs)->%*;
101  
102 while ( my ($dirpath, $dirindex) = each %index ) {
103 my $hash = cityhash64 $dirpath;
104 $entries{$hash}{__data} = $dirindex;
105 $entries{$hash}{is_dir} = true;
106 }
107  
108 delete $index{''};
109 @dirs = sort keys %index;
110 }
111  
112  
113 method list_dirs () {
114 @dirs
115 }
116  
117  
118 method list_files () {
119 @files
120 }
121  
122  
123 method entry_meta ($hash) {
124 $entries{ $hash }
125 }
126  
127  
128 method read_entry ($hash) {
129 my $entry = $entries{ $hash } or croak
130 sprintf "'%s': no entry for hash value %s", $path->basename, $hash;
131  
132 $entry->{__data}
133 }
134  
135  
136 method entries () {
137 keys %entries
138 }
139  
140  
141 sub create_file ($zipname, $scs, $file_opts = {}) {
142 $scs isa Archive::SCS or croak "Archive::SCS instance required";
143  
144 my $description;
145 my $comment = Path::Tiny->new($zipname)->basename;
146 if ( my $manifest = eval { $scs->read_entry('manifest.sii') } ) {
147 $manifest =~ m/package_version: \s*"(.+?)"/ax and $comment .= " version $1";
148 $manifest =~ m/author: \s*"(.+?)"/ax and $comment .= " by $1";
149 $manifest =~ m/description_file: \s*"(.+?)"/ax and $description = $1;
150 }
151  
152 # ZIP file format specification 4.1.11:
153 # The manifest file SHOULD be the first file in the ZIP file
154 $file_opts->{'manifest.sii'} //= {
155 zip_opts => { Method => ZIP_CM_STORE },
156 order => chr 0,
157 };
158  
159 # Make the mod description the second file, directly after the manifest
160 # (uncompressed so that it's readable when the file is viewed in binary)
161 defined $description and $file_opts->{$description} //= {
162 zip_opts => { Method => ZIP_CM_STORE },
163 order => chr 1,
164 };
165  
166 # Add records for empty directories (non-empty dirs are implicit in ZIP)
167 my @dirs;
168 for my $dir ($scs->list_dirs) {
169 my $index = $scs->read_entry($dir);
170 push @dirs, $dir unless $index->dirs || $index->files;
171 }
172  
173 my @entries = sort {
174 ($file_opts->{$a}{order} // $a) cmp ($file_opts->{$b}{order} // $b)
175 } @dirs, $scs->list_files;
176 @entries or croak "Creating empty ZIP files unimplemented";
177  
178 my $zip;
179 for my $entry (@entries) {
180 my $data = $scs->read_entry($entry);
181 utf8::upgrade($entry);
182  
183 if ($data isa Archive::SCS::DirIndex) {
184 $file_opts->{$entry}{zip_opts} //= {
185 ExtAttr => (0o40755 << 16) | 0x10, # Unix and DOS directory flags
186 Method => ZIP_CM_STORE,
187 Name => "$entry/",
188 };
189 $data = '';
190 }
191  
192 my %zip_opts = (
193 Efs => 1,
194 ExtAttr => $IO::Compress::Zip::PARAMS{ExtAttr},
195 Level => Z_BEST_COMPRESSION,
196 Method => ZIP_CM_DEFLATE,
197 Name => $entry,
198 Stream => 0,
199 TextFlag => !!( $entry =~ m/\.(sii|sui|txt)\z/n ),
200 ZipComment => $comment,
201 ( $file_opts->{$entry}{zip_opts} // {} )->%*,
202 );
203 utf8::encode($zip_opts{Name});
204  
205 if ($zip) {
206 $zip->newStream(%zip_opts);
207 }
208 else {
209 $zip = IO::Compress::Zip->new("$zipname", %zip_opts)
210 or die "IO::Compress::Zip failed: $ZipError\n";
211 }
212  
213 $zip->write($data);
214 }
215 $zip->close;
216 }
217  
218 1;
219  
220  
221 =head1 NAME
222  
223 Archive::SCS::Zip - ZIP format handler
224  
225 =head1 SYNOPSIS
226  
227 my $path = Path::Tiny->new('.../mod.scs');
228 my $archive = Archive::SCS::Zip->new(path => $path)->mount;
229  
230 $archive->read_dir_tree;
231 my @contents = sort $archive->list_dirs, $archive->list_files;
232  
233 my $hash = Archive::SCS::CityHash::cityhash64('manifest.sii');
234 my $data = $archive->read_entry($hash);
235  
236 =head1 DESCRIPTION
237  
238 Represents an SCS archive stored in ZIP file format.
239  
240 Hash values used with this module must be in the internal format
241 (currently, a 16-byte hex scalar in network byte order).
242  
243 =head1 METHODS
244  
245 =head2 entries
246  
247 $entry_hashes = $archive->entries;
248  
249 =head2 is_mounted
250  
251 $bool = $archive->is_mounted;
252  
253 =head2 handles_path
254  
255 $bool = Archive::SCS::Zip->handles_path($path_tiny, $header);
256  
257 =head2 list_dirs
258  
259 @directories = $archive->list_dirs;
260  
261 =head2 list_files
262  
263 @files = $archive->list_files;
264  
265 =head2 mount
266  
267 $archive = $archive->mount;
268  
269 =head2 new
270  
271 $archive = Archive::SCS::Zip->new(file => $path_tiny);
272  
273 =head2 path
274  
275 $path_tiny = $archive->path;
276  
277 =head2 read_dir_tree
278  
279 $archive->read_dir_tree;
280  
281 =head2 read_entry
282  
283 $data = $archive->read_entry($hash);
284  
285 =head2 unmount
286  
287 $archive->unmount;
288  
289 =head1 LIMITATIONS
290  
291 Mounting an archive reads all contents into memory, which can be
292 inefficient for large ZIP files on slow disks.
293  
294 Some invalid (and, rarely, valid) ZIP files that are accepted by
295 SCS's software cannot be mounted by this module. However, all plain
296 ZIP files that conform to the "appnote" spec should work just fine.
297  
298 =head1 AUTHOR
299  
300 L<nautofon|https://github.com/nautofon>
301  
302 =head1 COPYRIGHT
303  
304 This software is copyright (c) 2026 by nautofon.
305  
306 This is free software; you can redistribute it and/or modify it under
307 the same terms as the Perl 5 programming language system itself.