bcv-vf/src/main/java/the/bytecode/club/bytecodeviewer/util/ZipUtils.java

202 lines
8 KiB
Java
Raw Normal View History

2019-04-17 06:45:15 +00:00
package the.bytecode.club.bytecodeviewer.util;
2021-04-12 20:19:12 +00:00
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
2021-04-12 22:31:22 +00:00
import java.util.Objects;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
/***************************************************************************
* Bytecode Viewer (BCV) - Java & Android Reverse Engineering Suite *
* Copyright (C) 2014 Kalen 'Konloch' Kinloch - http://bytecodeviewer.com *
* *
* 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. *
* *
* 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. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
/**
* Rudimentary utility class for Zip archives.
*/
public final class ZipUtils {
2018-01-31 15:41:24 +00:00
/**
* Unzip files to path.
*
2021-04-12 20:19:12 +00:00
* @param jarPath the zip file name
2018-01-31 15:41:24 +00:00
* @param destinationDir the file extract path
* @throws IOException Signals that an I/O exception has occurred.
*/
public static void unzipFilesToPath(String jarPath, String destinationDir) throws IOException {
File file = new File(jarPath);
2021-07-25 16:54:08 +00:00
try (JarFile jar = new JarFile(file)) {
2018-01-31 15:41:24 +00:00
2021-07-25 16:54:08 +00:00
// fist get all directories,
// then make those directory on the destination Path
/*for (Enumeration<JarEntry> enums = jar.entries(); enums.hasMoreElements(); ) {
JarEntry entry = (JarEntry) enums.nextElement();
2018-01-31 15:41:24 +00:00
2021-07-25 16:54:08 +00:00
String fileName = destinationDir + File.separator + entry.getName();
File f = new File(fileName);
2018-01-31 15:41:24 +00:00
2021-07-25 16:54:08 +00:00
if (fileName.endsWith("/")) {
f.mkdirs();
}
2018-01-31 15:41:24 +00:00
2021-07-25 16:54:08 +00:00
}*/
2018-01-31 15:41:24 +00:00
2021-07-25 16:54:08 +00:00
//now create all files
for (Enumeration<JarEntry> enums = jar.entries(); enums.hasMoreElements(); ) {
JarEntry entry = enums.nextElement();
2019-06-01 01:04:07 +00:00
2021-07-25 16:54:08 +00:00
String fileName = destinationDir + File.separator + entry.getName();
File f = new File(fileName);
2018-01-31 15:41:24 +00:00
2021-07-25 16:54:08 +00:00
File parent = f.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
2018-01-31 15:41:24 +00:00
}
2021-07-25 16:54:08 +00:00
if (!fileName.endsWith("/")) {
try (InputStream is = jar.getInputStream(entry);
FileOutputStream fos = new FileOutputStream(f)) {
// write contents of 'is' to 'fos'
while (is.available() > 0) {
fos.write(is.read());
}
}
}
2018-01-31 15:41:24 +00:00
}
}
}
public static void zipFile(File inputFile, File outputZip) {
byte[] buffer = new byte[1024];
2021-07-25 16:54:08 +00:00
try (FileOutputStream fos = new FileOutputStream(outputZip);
ZipOutputStream zos = new ZipOutputStream(fos)) {
2018-01-31 15:41:24 +00:00
ZipEntry ze = new ZipEntry(inputFile.getName());
zos.putNextEntry(ze);
2021-07-25 16:54:08 +00:00
try (FileInputStream in = new FileInputStream(inputFile)) {
int len;
while ((len = in.read(buffer)) > 0) {
zos.write(buffer, 0, len);
}
2018-01-31 15:41:24 +00:00
}
zos.closeEntry();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void zipFolder(String srcFolder, String destZipFile, String ignore) throws Exception {
2021-07-25 16:54:08 +00:00
try (FileOutputStream fileWriter = new FileOutputStream(destZipFile);
ZipOutputStream zip = new ZipOutputStream(fileWriter)){
addFolderToZip("", srcFolder, zip, ignore);
zip.flush();
}
2018-01-31 15:41:24 +00:00
}
2019-06-01 01:04:07 +00:00
public static void zipFolderAPKTool(String srcFolder, String destZipFile) throws Exception {
2021-07-25 16:54:08 +00:00
try (FileOutputStream fileWriter = new FileOutputStream(destZipFile);
ZipOutputStream zip = new ZipOutputStream(fileWriter)){
addFolderToZipAPKTool("", srcFolder, zip);
zip.flush();
}
2019-06-01 01:04:07 +00:00
}
2018-01-31 15:41:24 +00:00
public static void addFileToZip(String path, String srcFile, ZipOutputStream zip, String ignore)
throws Exception {
File folder = new File(srcFile);
if (folder.isDirectory()) {
addFolderToZip(path, srcFile, zip, ignore);
} else {
byte[] buf = new byte[1024];
int len;
2021-07-25 16:54:08 +00:00
try (FileInputStream in = new FileInputStream(srcFile)) {
ZipEntry entry;
if (ignore == null)
entry = new ZipEntry(path + "/" + folder.getName());
else
entry = new ZipEntry(path.replace(ignore, "BCV_Krakatau") + "/" + folder.getName());
zip.putNextEntry(entry);
while ((len = in.read(buf)) > 0) {
zip.write(buf, 0, len);
}
2018-01-31 15:41:24 +00:00
}
}
}
2021-04-12 20:19:12 +00:00
public static void addFileToZipAPKTool(String path, String srcFile, ZipOutputStream zip) throws Exception {
2019-06-01 01:04:07 +00:00
File folder = new File(srcFile);
String check = path.toLowerCase();
2021-04-12 20:19:12 +00:00
//if(check.startsWith("decoded unknown") || check.startsWith("decoded lib") || check.startsWith("decoded
// assets") || check.startsWith("decoded original") || check.startsWith("decoded smali") || check.startsWith
// ("decoded apktool.yml"))
if (check.startsWith("decoded original") || check.startsWith("decoded smali") || check.startsWith("decoded "
+ "apktool.yml"))
2019-06-01 01:04:07 +00:00
return;
//if(path.equals("original") || path.equals("classes.dex") || path.equals("apktool.yml"))
// continue;
2021-04-12 20:19:12 +00:00
if (folder.isDirectory()) {
2019-06-01 01:04:07 +00:00
addFolderToZipAPKTool(path, srcFile, zip);
} else {
byte[] buf = new byte[1024];
int len;
2021-07-25 16:54:08 +00:00
try (FileInputStream in = new FileInputStream(srcFile)) {
ZipEntry entry;
2019-06-01 01:04:07 +00:00
2021-07-25 16:54:08 +00:00
entry = new ZipEntry(path + "/" + folder.getName());
zip.putNextEntry(entry);
2019-06-01 01:04:07 +00:00
2021-07-25 16:54:08 +00:00
while ((len = in.read(buf)) > 0) {
zip.write(buf, 0, len);
}
2019-06-01 01:04:07 +00:00
}
}
}
2018-01-31 15:41:24 +00:00
public static void addFolderToZip(String path, String srcFolder, ZipOutputStream zip, String ignore)
throws Exception {
File folder = new File(srcFolder);
2021-04-12 22:31:22 +00:00
for (String fileName : Objects.requireNonNull(folder.list())) {
if (path.isEmpty()) {
2018-01-31 15:41:24 +00:00
addFileToZip(folder.getName(), srcFolder + "/" + fileName, zip, ignore);
} else {
addFileToZip(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip, ignore);
}
}
}
2019-06-01 01:04:07 +00:00
2021-04-12 20:19:12 +00:00
public static void addFolderToZipAPKTool(String path, String srcFolder, ZipOutputStream zip) throws Exception {
2019-06-01 01:04:07 +00:00
File folder = new File(srcFolder);
2021-04-12 22:31:22 +00:00
for (String fileName : Objects.requireNonNull(folder.list())) {
if (path.isEmpty()) {
2019-06-01 01:04:07 +00:00
addFileToZipAPKTool(folder.getName(), srcFolder + "/" + fileName, zip);
} else {
addFileToZipAPKTool(path + "/" + folder.getName(), srcFolder + "/" + fileName, zip);
}
}
}
}