@@ -85,53 +85,49 @@ public boolean isExtracted() {
8585 *
8686 * @param stream The input stream.
8787 */
88- private void readCrate (InputStream stream ) {
89- try {
90- File folder = temporaryFolder .toFile ();
91- // ensure the directory is clean
92- if (folder .exists ()) {
93- if (folder .isDirectory ()) {
94- FileUtils .cleanDirectory (folder );
95- } else if (folder .isFile ()) {
96- FileUtils .delete (folder );
97- }
98- } else {
99- FileUtils .forceMkdir (folder );
88+ private void readCrate (InputStream stream ) throws IOException {
89+ File folder = temporaryFolder .toFile ();
90+ // ensure the directory is clean
91+ if (folder .exists ()) {
92+ if (folder .isDirectory ()) {
93+ FileUtils .cleanDirectory (folder );
94+ } else if (folder .isFile ()) {
95+ FileUtils .delete (folder );
10096 }
97+ } else {
98+ FileUtils .forceMkdir (folder );
99+ }
101100
102- LocalFileHeader localFileHeader ;
103- int readLen ;
104- byte [] readBuffer = new byte [4096 ];
101+ LocalFileHeader localFileHeader ;
102+ int readLen ;
103+ byte [] readBuffer = new byte [4096 ];
105104
106- try (ZipInputStream zipInputStream = new ZipInputStream (stream )) {
107- while ((localFileHeader = zipInputStream .getNextEntry ()) != null ) {
108- String fileName = localFileHeader .getFileName ();
109- File extractedFile = new File (folder , fileName ).getCanonicalFile ();
110- if (!extractedFile .toPath ().startsWith (folder .getCanonicalPath ())) {
111- throw new IOException ("Entry is outside of target directory: " + fileName );
112- }
113- if (localFileHeader .isDirectory ()) {
114- FileUtils .forceMkdir (extractedFile );
115- continue ;
116- }
117- FileUtils .forceMkdir (extractedFile .getParentFile ());
118- try (OutputStream outputStream = new FileOutputStream (extractedFile )) {
119- while ((readLen = zipInputStream .read (readBuffer )) != -1 ) {
120- outputStream .write (readBuffer , 0 , readLen );
121- }
105+ try (ZipInputStream zipInputStream = new ZipInputStream (stream )) {
106+ while ((localFileHeader = zipInputStream .getNextEntry ()) != null ) {
107+ String fileName = localFileHeader .getFileName ();
108+ File extractedFile = new File (folder , fileName ).getCanonicalFile ();
109+ if (!extractedFile .toPath ().startsWith (folder .getCanonicalPath ())) {
110+ throw new IOException ("Entry is outside of target directory: " + fileName );
111+ }
112+ if (localFileHeader .isDirectory ()) {
113+ FileUtils .forceMkdir (extractedFile );
114+ continue ;
115+ }
116+ FileUtils .forceMkdir (extractedFile .getParentFile ());
117+ try (OutputStream outputStream = new FileOutputStream (extractedFile )) {
118+ while ((readLen = zipInputStream .read (readBuffer )) != -1 ) {
119+ outputStream .write (readBuffer , 0 , readLen );
122120 }
123121 }
124122 }
125- this .isExtracted = true ;
126- // register deletion on exit
127- FileUtils .forceDeleteOnExit (folder );
128- } catch (IOException ex ) {
129- logger .error ("Failed to read crate from input stream." , ex );
130123 }
124+ this .isExtracted = true ;
125+ // register deletion on exit
126+ FileUtils .forceDeleteOnExit (folder );
131127 }
132128
133129 @ Override
134- public ObjectNode readMetadataJson (InputStream stream ) {
130+ public ObjectNode readMetadataJson (InputStream stream ) throws IOException {
135131 if (!isExtracted ) {
136132 this .readCrate (stream );
137133 }
@@ -152,17 +148,11 @@ public ObjectNode readMetadataJson(InputStream stream) {
152148 .orElseThrow (() -> new IllegalStateException ("No %s found in zip file" .formatted (JsonDescriptor .ID )));
153149 jsonMetadata = firstSubdir .toPath ().resolve (JsonDescriptor .ID ).toFile ();
154150 }
155-
156- try {
157- return objectMapper .readTree (jsonMetadata ).deepCopy ();
158- } catch (IOException e ) {
159- logger .error ("Failed to deserialize crate metadata." , e );
160- return null ;
161- }
151+ return objectMapper .readTree (jsonMetadata ).deepCopy ();
162152 }
163153
164154 @ Override
165- public File readContent (InputStream stream ) {
155+ public File readContent (InputStream stream ) throws IOException {
166156 if (!isExtracted ) {
167157 this .readCrate (stream );
168158 }
0 commit comments