11package de .uni_stuttgart .ils .reqif4j .reqif ;
22
3+ import javax .swing .filechooser .FileNameExtensionFilter ;
34import java .io .*;
5+ import java .nio .file .Path ;
6+ import java .nio .file .Paths ;
47import java .util .HashMap ;
58import java .util .Map ;
69import java .util .zip .ZipEntry ;
710import java .util .zip .ZipInputStream ;
811
912public class ReqIFz extends ReqIFFile {
13+ private String extendFilename = "_unzipped" ;
1014
1115 public ReqIFz (String filePath ) throws IOException {
12- File destDir = new File (filePath + "_unzipped" ); // Extract to a separate directory
16+ File destDir = new File (filePath );
17+ String pathWithoutFileExtension = removeFileExtension (destDir .getAbsolutePath (), true );
18+ destDir = new File (pathWithoutFileExtension + extendFilename );
1319 if (!destDir .exists () && !destDir .mkdirs ()) {
1420 throw new IOException ("Failed to create extraction directory " + destDir );
1521 }
1622
1723 byte [] buffer = new byte [1024 ];
24+ Map <String , InputStream > picturesIS = new HashMap <>();
25+
1826 try (ZipInputStream zis = new ZipInputStream (new FileInputStream (filePath ))) {
1927 ZipEntry zipEntry = zis .getNextEntry ();
2028
@@ -43,26 +51,27 @@ public ReqIFz(String filePath) throws IOException {
4351 if (zipEntry .getName ().endsWith ("reqif" )) {
4452 this .numberOfReqIFDocuments ++;
4553 String reqifBaseName = zipEntry .getName ().split ("\\ ." )[0 ];
46- Map <String , InputStream > picturesIS = new HashMap <>();
47-
48- // Iterate again for images within the same loop
49- try (ZipInputStream imageZis = new ZipInputStream (new FileInputStream (filePath ))) {
50- ZipEntry imageEntry = imageZis .getNextEntry ();
51- while (imageEntry != null ) {
52- String pictureFileName = new File (imageEntry .getName ()).getName ();
53- if (pictureFileName .endsWith ("png" ) || pictureFileName .endsWith ("jpeg" ) || pictureFileName .endsWith ("jpg" )) {
54- picturesIS .put (pictureFileName , new FileInputStream (new File (destDir , imageEntry .getName ())));
55- }
56- imageEntry = imageZis .getNextEntry ();
57- }
58- }
59-
60- this .picturesInReqIFDocument .put (reqifBaseName , picturesIS );
6154 this .reqifDocuments .put (zipEntry .getName (), new ReqIFDocument (new FileInputStream (newFile ), filePath , zipEntry .getName ()));
55+ } else if (zipEntry .getName ().endsWith ("png" ) || zipEntry .getName ().endsWith ("jpeg" ) || zipEntry .getName ().endsWith ("jpg" )) {
56+ picturesIS .put (zipEntry .getName (), new FileInputStream (newFile ));
6257 }
6358 }
6459 zipEntry = zis .getNextEntry ();
6560 }
6661 }
62+
63+ // Assuming you want to associate pictures with ReqIF documents
64+ for (String reqifBaseName : this .reqifDocuments .keySet ()) {
65+ this .picturesInReqIFDocument .put (reqifBaseName , picturesIS );
66+ }
67+ }
68+
69+ public static String removeFileExtension (String filename , boolean removeAllExtensions ) {
70+ if (filename == null || filename .isEmpty ()) {
71+ return filename ;
72+ }
73+
74+ String extPattern = "(?<!^)[.]" + (removeAllExtensions ? ".*" : "[^.]*$" );
75+ return filename .replaceAll (extPattern , "" );
6776 }
68- }
77+ }
0 commit comments