mirror of
https://github.com/revanced/Apktool.git
synced 2025-05-01 14:44:26 +02:00
Merge branch 'rover12421-del_BrutIO_copy'
This commit is contained in:
commit
ba9b4af973
@ -36,6 +36,7 @@ import java.util.zip.ZipEntry;
|
|||||||
import java.util.zip.ZipFile;
|
import java.util.zip.ZipFile;
|
||||||
import java.util.zip.ZipOutputStream;
|
import java.util.zip.ZipOutputStream;
|
||||||
|
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.yaml.snakeyaml.DumperOptions;
|
import org.yaml.snakeyaml.DumperOptions;
|
||||||
import org.yaml.snakeyaml.Yaml;
|
import org.yaml.snakeyaml.Yaml;
|
||||||
|
|
||||||
@ -230,7 +231,7 @@ public class Androlib {
|
|||||||
|
|
||||||
try (
|
try (
|
||||||
Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
|
Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
|
||||||
new File(mOutDir, "apktool.yml")), "UTF-8"));
|
new File(mOutDir, "apktool.yml")), "UTF-8"))
|
||||||
) {
|
) {
|
||||||
yaml.dump(meta, writer);
|
yaml.dump(meta, writer);
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
@ -241,7 +242,7 @@ public class Androlib {
|
|||||||
public Map<String, Object> readMetaFile(ExtFile appDir)
|
public Map<String, Object> readMetaFile(ExtFile appDir)
|
||||||
throws AndrolibException {
|
throws AndrolibException {
|
||||||
try(
|
try(
|
||||||
InputStream in = appDir.getDirectory().getFileInput("apktool.yml");
|
InputStream in = appDir.getDirectory().getFileInput("apktool.yml")
|
||||||
) {
|
) {
|
||||||
Yaml yaml = new Yaml();
|
Yaml yaml = new Yaml();
|
||||||
return (Map<String, Object>) yaml.load(in);
|
return (Map<String, Object>) yaml.load(in);
|
||||||
@ -408,8 +409,7 @@ public class Androlib {
|
|||||||
if (apkOptions.forceBuildAll || isModified(newFiles(APK_RESOURCES_FILENAMES, appDir),
|
if (apkOptions.forceBuildAll || isModified(newFiles(APK_RESOURCES_FILENAMES, appDir),
|
||||||
newFiles(APK_RESOURCES_FILENAMES, apkDir))) {
|
newFiles(APK_RESOURCES_FILENAMES, apkDir))) {
|
||||||
LOGGER.info("Copying raw resources...");
|
LOGGER.info("Copying raw resources...");
|
||||||
appDir.getDirectory()
|
appDir.getDirectory().copyToDir(apkDir, APK_RESOURCES_FILENAMES);
|
||||||
.copyToDir(apkDir, APK_RESOURCES_FILENAMES);
|
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
} catch (DirectoryException ex) {
|
} catch (DirectoryException ex) {
|
||||||
@ -481,7 +481,7 @@ public class Androlib {
|
|||||||
File apkDir = new File(appDir, APK_DIRNAME);
|
File apkDir = new File(appDir, APK_DIRNAME);
|
||||||
|
|
||||||
if (apkOptions.debugMode) {
|
if (apkOptions.debugMode) {
|
||||||
mAndRes.remove_application_debug(new File(apkDir,"AndroidManifest.xml").getAbsolutePath());
|
mAndRes.remove_application_debug(new File(apkDir, "AndroidManifest.xml").getAbsolutePath());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (apkOptions.forceBuildAll || isModified(newFiles(APK_MANIFEST_FILENAMES, appDir),
|
if (apkOptions.forceBuildAll || isModified(newFiles(APK_MANIFEST_FILENAMES, appDir),
|
||||||
@ -568,11 +568,10 @@ public class Androlib {
|
|||||||
|
|
||||||
try (
|
try (
|
||||||
ZipFile inputFile = new ZipFile(tempFile);
|
ZipFile inputFile = new ZipFile(tempFile);
|
||||||
ZipOutputStream actualOutput = new ZipOutputStream(new FileOutputStream(outFile));
|
ZipOutputStream actualOutput = new ZipOutputStream(new FileOutputStream(outFile))
|
||||||
) {
|
) {
|
||||||
byte[] buffer = new byte[4096 * 1024];
|
copyExistingFiles(inputFile, actualOutput);
|
||||||
copyExistingFiles(inputFile, actualOutput, buffer);
|
copyUnknownFiles(appDir, actualOutput, files);
|
||||||
copyUnknownFiles(appDir, actualOutput, files, buffer);
|
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
throw new AndrolibException(ex);
|
throw new AndrolibException(ex);
|
||||||
}
|
}
|
||||||
@ -582,54 +581,52 @@ public class Androlib {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void copyExistingFiles(ZipFile inputFile, ZipOutputStream outputFile, byte[] buffer) throws IOException {
|
private void copyExistingFiles(ZipFile inputFile, ZipOutputStream outputFile) throws IOException {
|
||||||
// First, copy the contents from the existing outFile:
|
// First, copy the contents from the existing outFile:
|
||||||
Enumeration<? extends ZipEntry> entries = inputFile.entries();
|
Enumeration<? extends ZipEntry> entries = inputFile.entries();
|
||||||
while (entries.hasMoreElements()) {
|
while (entries.hasMoreElements()) {
|
||||||
ZipEntry entry = new ZipEntry(entries.nextElement());
|
ZipEntry entry = new ZipEntry(entries.nextElement());
|
||||||
|
|
||||||
// We can't reuse the compressed size because it depends on compression sizes.
|
// We can't reuse the compressed size because it depends on compression sizes.
|
||||||
entry.setCompressedSize(-1);
|
entry.setCompressedSize(-1);
|
||||||
outputFile.putNextEntry(entry);
|
outputFile.putNextEntry(entry);
|
||||||
|
|
||||||
// No need to create directory entries in the final apk
|
// No need to create directory entries in the final apk
|
||||||
if (!entry.isDirectory()) {
|
if (! entry.isDirectory()) {
|
||||||
BrutIO.copy(inputFile.getInputStream(entry), outputFile, buffer);
|
BrutIO.copy(inputFile, outputFile, entry);
|
||||||
}
|
}
|
||||||
|
|
||||||
outputFile.closeEntry();
|
outputFile.closeEntry();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void copyUnknownFiles(File appDir, ZipOutputStream outputFile, Map<String, String> files, byte[] buffer)
|
private void copyUnknownFiles(File appDir, ZipOutputStream outputFile, Map<String, String> files)
|
||||||
throws IOException {
|
throws IOException {
|
||||||
File unknownFileDir = new File(appDir, UNK_DIRNAME);
|
File unknownFileDir = new File(appDir, UNK_DIRNAME);
|
||||||
|
|
||||||
// loop through unknown files
|
// loop through unknown files
|
||||||
for (Map.Entry<String,String> unknownFileInfo : files.entrySet()) {
|
for (Map.Entry<String,String> unknownFileInfo : files.entrySet()) {
|
||||||
File inputFile = new File(unknownFileDir, unknownFileInfo.getKey());
|
File inputFile = new File(unknownFileDir, unknownFileInfo.getKey());
|
||||||
if(inputFile.isDirectory()) {
|
if (inputFile.isDirectory()) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
ZipEntry newEntry = new ZipEntry(unknownFileInfo.getKey());
|
ZipEntry newEntry = new ZipEntry(unknownFileInfo.getKey());
|
||||||
int method = Integer.valueOf(unknownFileInfo.getValue());
|
int method = Integer.valueOf(unknownFileInfo.getValue());
|
||||||
LOGGER.fine(String.format("Copying unknown file %s with method %d", unknownFileInfo.getKey(), method));
|
LOGGER.fine(String.format("Copying unknown file %s with method %d", unknownFileInfo.getKey(), method));
|
||||||
if(method == ZipEntry.STORED) {
|
if (method == ZipEntry.STORED) {
|
||||||
newEntry.setMethod(ZipEntry.STORED);
|
newEntry.setMethod(ZipEntry.STORED);
|
||||||
newEntry.setSize(inputFile.length());
|
newEntry.setSize(inputFile.length());
|
||||||
newEntry.setCompressedSize(-1);
|
newEntry.setCompressedSize(-1);
|
||||||
BufferedInputStream unknownFile = new BufferedInputStream(new FileInputStream(inputFile));
|
BufferedInputStream unknownFile = new BufferedInputStream(new FileInputStream(inputFile));
|
||||||
CRC32 crc = BrutIO.calculateCrc(unknownFile, buffer);
|
CRC32 crc = BrutIO.calculateCrc(unknownFile);
|
||||||
newEntry.setCrc(crc.getValue());
|
newEntry.setCrc(crc.getValue());
|
||||||
|
|
||||||
LOGGER.fine("\tsize: " + newEntry.getSize());
|
|
||||||
} else {
|
} else {
|
||||||
newEntry.setMethod(ZipEntry.DEFLATED);
|
newEntry.setMethod(ZipEntry.DEFLATED);
|
||||||
}
|
}
|
||||||
outputFile.putNextEntry(newEntry);
|
outputFile.putNextEntry(newEntry);
|
||||||
|
|
||||||
BufferedInputStream unknownFile = new BufferedInputStream(new FileInputStream(inputFile));
|
BrutIO.copy(inputFile, outputFile);
|
||||||
BrutIO.copy(unknownFile, outputFile, buffer);
|
|
||||||
outputFile.closeEntry();
|
outputFile.closeEntry();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,9 @@ package brut.util;
|
|||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.util.zip.CRC32;
|
import java.util.zip.CRC32;
|
||||||
|
import java.util.zip.ZipEntry;
|
||||||
|
import java.util.zip.ZipFile;
|
||||||
|
import java.util.zip.ZipOutputStream;
|
||||||
|
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
|
|
||||||
@ -30,10 +33,8 @@ public class BrutIO {
|
|||||||
try {
|
try {
|
||||||
IOUtils.copy(in, out);
|
IOUtils.copy(in, out);
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
IOUtils.closeQuietly(in);
|
||||||
in.close();
|
IOUtils.closeQuietly(out);
|
||||||
out.close();
|
|
||||||
} catch (IOException ex) {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,19 +63,30 @@ public class BrutIO {
|
|||||||
return modified;
|
return modified;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static CRC32 calculateCrc(InputStream input, byte[] buffer) throws IOException {
|
public static CRC32 calculateCrc(InputStream input) throws IOException {
|
||||||
CRC32 crc = new CRC32();
|
CRC32 crc = new CRC32();
|
||||||
int bytesRead = 0;
|
int bytesRead;
|
||||||
|
byte[] buffer = new byte[8192];
|
||||||
while((bytesRead = input.read(buffer)) != -1) {
|
while((bytesRead = input.read(buffer)) != -1) {
|
||||||
crc.update(buffer, 0, bytesRead);
|
crc.update(buffer, 0, bytesRead);
|
||||||
}
|
}
|
||||||
return crc;
|
return crc;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void copy(InputStream input, OutputStream output, byte[] buffer) throws IOException {
|
public static void copy(File inputFile, ZipOutputStream outputFile) throws IOException {
|
||||||
int bytesRead;
|
try (
|
||||||
while((bytesRead = input.read(buffer)) != -1) {
|
FileInputStream fis = new FileInputStream(inputFile)
|
||||||
output.write(buffer, 0, bytesRead);
|
) {
|
||||||
|
IOUtils.copy(fis, outputFile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void copy(ZipFile inputFile, ZipOutputStream outputFile, ZipEntry entry) throws IOException {
|
||||||
|
try (
|
||||||
|
InputStream is = inputFile.getInputStream(entry)
|
||||||
|
) {
|
||||||
|
IOUtils.copy(is, outputFile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user