@@ -242,15 +242,19 @@ public void onCopyProgress(long bytesRead) {
242242 @ ReactMethod
243243 public void zip (String fileOrDirectory , String destDirectory , Promise promise ) {
244244 List <String > filePaths = new ArrayList <>();
245+
246+ String fromDirectory ;
245247 try {
246248 File tmp = new File (fileOrDirectory );
247249 if (tmp .exists ()) {
248250 if (tmp .isDirectory ()) {
251+ fromDirectory = fileOrDirectory ;
249252 List <File > files = getSubFiles (tmp , true );
250253 for (int i = 0 ; i < files .size (); i ++) {
251254 filePaths .add (files .get (i ).getAbsolutePath ());
252255 }
253256 } else {
257+ fromDirectory = fileOrDirectory .substring (0 , fileOrDirectory .lastIndexOf ("/" ));
254258 filePaths .add (fileOrDirectory );
255259 }
256260 } else {
@@ -262,7 +266,8 @@ public void zip(String fileOrDirectory, String destDirectory, Promise promise) {
262266 }
263267
264268 try {
265- zipStream (filePaths .toArray (new String [filePaths .size ()]), destDirectory , filePaths .size ());
269+ String [] filePathArray = filePaths .toArray (new String [filePaths .size ()]);
270+ zipStream (filePathArray , destDirectory , fromDirectory , filePaths .size ());
266271 } catch (Exception ex ) {
267272 promise .reject (null , ex .getMessage ());
268273 return ;
@@ -271,7 +276,7 @@ public void zip(String fileOrDirectory, String destDirectory, Promise promise) {
271276 promise .resolve (destDirectory );
272277 }
273278
274- private void zipStream (String [] files , String destFile , @ SuppressWarnings ("UnusedParameters" ) long totalSize ) throws Exception {
279+ private void zipStream (String [] files , String destFile , String fromDirectory , @ SuppressWarnings ("UnusedParameters" ) long totalSize ) throws Exception {
275280 try {
276281 if (destFile .contains ("/" )) {
277282 File destDir = new File (destFile .substring (0 , destFile .lastIndexOf ("/" )));
@@ -295,11 +300,13 @@ private void zipStream(String[] files, String destFile, @SuppressWarnings("Unuse
295300
296301 updateProgress (0 , 1 , destFile ); // force 0%
297302 for (int i = 0 ; i < files .length ; i ++) {
298- FileInputStream fi = new FileInputStream (files [i ]);
299- String filename = files [i ].substring (files [i ].lastIndexOf ("/" ) + 1 );
300- ZipEntry entry = new ZipEntry (filename );
301- out .putNextEntry (entry );
302- if (!new File (files [i ]).isDirectory ()) {
303+ String absoluteFilepath = files [i ];
304+
305+ if (!new File (absoluteFilepath ).isDirectory ()) {
306+ FileInputStream fi = new FileInputStream (absoluteFilepath );
307+ String filename = absoluteFilepath .replace (fromDirectory , "" );
308+ ZipEntry entry = new ZipEntry (filename );
309+ out .putNextEntry (entry );
303310 origin = new BufferedInputStream (fi , BUFFER_SIZE );
304311 int count ;
305312 while ((count = origin .read (data , 0 , BUFFER_SIZE )) != -1 ) {
0 commit comments