2018-01-28 19:02:34 +01:00
|
|
|
package org.schabi.newpipe.util;
|
|
|
|
|
|
|
|
import java.io.BufferedInputStream;
|
2018-01-28 21:03:01 +01:00
|
|
|
import java.io.File;
|
2018-01-28 19:02:34 +01:00
|
|
|
import java.io.FileInputStream;
|
2018-01-28 21:03:01 +01:00
|
|
|
import java.io.FileOutputStream;
|
2018-01-28 19:02:34 +01:00
|
|
|
import java.util.zip.ZipEntry;
|
2018-01-28 21:03:01 +01:00
|
|
|
import java.util.zip.ZipInputStream;
|
2018-01-28 19:02:34 +01:00
|
|
|
import java.util.zip.ZipOutputStream;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Created by Christian Schabesberger on 28.01.18.
|
|
|
|
* Copyright 2018 Christian Schabesberger <chris.schabesberger@mailbox.org>
|
|
|
|
* ZipHelper.java is part of NewPipe
|
2020-03-31 19:20:15 +02:00
|
|
|
* <p>
|
2018-01-28 19:02:34 +01:00
|
|
|
* License: GPL-3.0+
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
2020-03-31 19:20:15 +02:00
|
|
|
* <p>
|
2018-01-28 19:02:34 +01:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2020-03-31 19:20:15 +02:00
|
|
|
* <p>
|
2018-01-28 19:02:34 +01:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
public final class ZipHelper {
|
|
|
|
private ZipHelper() { }
|
2018-01-28 19:02:34 +01:00
|
|
|
|
|
|
|
private static final int BUFFER_SIZE = 2048;
|
|
|
|
|
|
|
|
/**
|
2018-01-28 21:03:01 +01:00
|
|
|
* This function helps to create zip files.
|
|
|
|
* Caution this will override the original file.
|
2020-03-31 19:20:15 +02:00
|
|
|
*
|
2018-01-28 19:02:34 +01:00
|
|
|
* @param outZip The ZipOutputStream where the data should be stored in
|
2020-03-31 19:20:15 +02:00
|
|
|
* @param file The path of the file that should be added to zip.
|
|
|
|
* @param name The path of the file inside the zip.
|
2018-01-28 19:02:34 +01:00
|
|
|
* @throws Exception
|
|
|
|
*/
|
2020-03-31 19:20:15 +02:00
|
|
|
public static void addFileToZip(final ZipOutputStream outZip, final String file,
|
|
|
|
final String name) throws Exception {
|
2020-08-16 10:24:58 +02:00
|
|
|
final byte[] data = new byte[BUFFER_SIZE];
|
2020-11-06 01:46:13 +01:00
|
|
|
try (FileInputStream fi = new FileInputStream(file);
|
|
|
|
BufferedInputStream inputStream = new BufferedInputStream(fi, BUFFER_SIZE)) {
|
|
|
|
final ZipEntry entry = new ZipEntry(name);
|
|
|
|
outZip.putNextEntry(entry);
|
|
|
|
int count;
|
|
|
|
while ((count = inputStream.read(data, 0, BUFFER_SIZE)) != -1) {
|
|
|
|
outZip.write(data, 0, count);
|
|
|
|
}
|
2018-01-28 19:02:34 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-28 21:03:01 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This will extract data from Zipfiles.
|
|
|
|
* Caution this will override the original file.
|
2020-03-31 19:20:15 +02:00
|
|
|
*
|
|
|
|
* @param filePath The path of the zip
|
2018-01-28 21:03:01 +01:00
|
|
|
* @param file The path of the file on the disk where the data should be extracted to.
|
|
|
|
* @param name The path of the file inside the zip.
|
|
|
|
* @return will return true if the file was found within the zip file
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2020-03-31 19:20:15 +02:00
|
|
|
public static boolean extractFileFromZip(final String filePath, final String file,
|
|
|
|
final String name) throws Exception {
|
2020-11-06 01:46:13 +01:00
|
|
|
try (ZipInputStream inZip = new ZipInputStream(new BufferedInputStream(
|
|
|
|
new FileInputStream(filePath)))) {
|
|
|
|
final byte[] data = new byte[BUFFER_SIZE];
|
2018-04-19 01:31:25 +02:00
|
|
|
|
2020-11-06 01:46:13 +01:00
|
|
|
boolean found = false;
|
2018-01-28 21:03:01 +01:00
|
|
|
|
2020-11-06 01:46:13 +01:00
|
|
|
ZipEntry ze;
|
|
|
|
while ((ze = inZip.getNextEntry()) != null) {
|
|
|
|
if (ze.getName().equals(name)) {
|
|
|
|
found = true;
|
|
|
|
// delete old file first
|
|
|
|
final File oldFile = new File(file);
|
|
|
|
if (oldFile.exists()) {
|
|
|
|
if (!oldFile.delete()) {
|
|
|
|
throw new Exception("Could not delete " + file);
|
|
|
|
}
|
|
|
|
}
|
2018-01-28 21:03:01 +01:00
|
|
|
|
2020-11-06 01:46:13 +01:00
|
|
|
try (FileOutputStream outFile = new FileOutputStream(file)) {
|
|
|
|
int count = 0;
|
|
|
|
while ((count = inZip.read(data)) != -1) {
|
|
|
|
outFile.write(data, 0, count);
|
|
|
|
}
|
2018-01-28 21:03:01 +01:00
|
|
|
}
|
|
|
|
|
2020-11-06 01:46:13 +01:00
|
|
|
inZip.closeEntry();
|
2018-01-28 21:03:01 +01:00
|
|
|
}
|
|
|
|
}
|
2020-11-06 01:46:13 +01:00
|
|
|
return found;
|
2018-01-28 21:03:01 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-28 19:02:34 +01:00
|
|
|
}
|