Skip to content

Commit

Permalink
Revert ZipService to depends on java.util.zip instead of zip4j.
Browse files Browse the repository at this point in the history
Fixes regression of corrupted zip files after migrated to zip4j. Additionally, sends a ACTION_MEDIA_SCANNER_SCAN_FILE intent so the generated zip file is visible when device is accessible as storage device over USB.
  • Loading branch information
TranceLove authored and EmmanuelMess committed Feb 15, 2020
1 parent 2fb97ed commit ae6ec4f
Showing 1 changed file with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.widget.RemoteViews;

import androidx.annotation.NonNull;
import androidx.annotation.StringRes;
import androidx.core.app.NotificationCompat;
import android.widget.RemoteViews;

import com.amaze.filemanager.R;
import com.amaze.filemanager.activities.MainActivity;
Expand All @@ -49,16 +51,16 @@
import com.amaze.filemanager.utils.files.FileUtils;
import com.amaze.filemanager.utils.files.GenericCopyUtil;

import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.io.ZipOutputStream;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.zip.ZipEntry;
import java.util.zip.ZipException;
import java.util.zip.ZipOutputStream;

public class ZipService extends AbstractProgressiveService {

Expand Down Expand Up @@ -263,12 +265,13 @@ public void execute(final @NonNull Context context, ArrayList<File> baseFiles, S
progressHandler.setSourceFilesProcessed(++fileProgress);
compressFile(file, "");
}
} catch (IOException | ZipException e) {
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
zos.flush();
zos.close();
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE).setData(Uri.fromFile(zipDirectory)));
} catch (IOException e) {
e.printStackTrace();
}
Expand All @@ -281,11 +284,18 @@ private void compressFile(File file, String path) throws IOException, NullPointe
if (!file.isDirectory()) {
byte[] buf = new byte[GenericCopyUtil.DEFAULT_BUFFER_SIZE];
int len;
ZipParameters parameters = new ZipParameters();
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
parameters.setFileNameInZip(path + "/" + file.getName());
zos.putNextEntry(file, parameters);
ServiceWatcherUtil.position += file.length();
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
zos.putNextEntry(new ZipEntry(path + "/" + file.getName()));
try {
while ((len = in.read(buf)) > 0) {
if (!progressHandler.getCancelled()) {
zos.write(buf, 0, len);
ServiceWatcherUtil.position += len;
} else break;
}
} finally {
in.close();
}
return;
}

Expand Down

0 comments on commit ae6ec4f

Please sign in to comment.