Skip to content

Commit

Permalink
fix: saves all resources (#375)
Browse files Browse the repository at this point in the history
  • Loading branch information
bagipro authored and skylot committed Oct 24, 2018
1 parent 87f50ab commit 8eef4a9
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 20 deletions.
17 changes: 0 additions & 17 deletions jadx-core/src/main/java/jadx/api/ResourceType.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,4 @@ public static ResourceType getFileType(String fileName) {
}
return UNKNOWN;
}

public static boolean isSupportedForUnpack(ResourceType type) {
switch (type) {
case CODE:
case LIB:
case FONT:
case UNKNOWN:
return false;

case MANIFEST:
case XML:
case ARSC:
case IMG:
return true;
}
return false;
}
}
6 changes: 6 additions & 0 deletions jadx-core/src/main/java/jadx/api/ResourcesLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ private static ResContainer loadContent(JadxDecompiler jadxRef, ResourceFile rf,
case IMG:
return ResContainer.singleImageFile(rf.getName(), inputStream);

case CODE:
case LIB:
case FONT:
case UNKNOWN:
return ResContainer.singleBinaryFile(rf.getName(), inputStream);

default:
if (size > LOAD_SIZE_LIMIT) {
return ResContainer.singleFile(rf.getName(),
Expand Down
20 changes: 20 additions & 0 deletions jadx-core/src/main/java/jadx/core/xmlgen/ResContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
Expand All @@ -30,6 +32,8 @@ public class ResContainer implements Comparable<ResContainer> {
private CodeWriter content;
@Nullable
private BufferedImage image;
@Nullable
private InputStream binary;

private ResContainer(String name, List<ResContainer> subFiles) {
this.name = name;
Expand Down Expand Up @@ -62,6 +66,17 @@ public static ResContainer singleImageFile(String name, InputStream content) {
}
return resContainer;
}

public static ResContainer singleBinaryFile(String name, InputStream content) {
ResContainer resContainer = new ResContainer(name, Collections.emptyList());
try {
resContainer.binary = new ByteArrayInputStream(IOUtils.toByteArray(content));
}
catch(IOException e) {
LOG.warn("Contents of the binary resource '{}' not saved, got exception {}", name, e);
}
return resContainer;
}

public static ResContainer multiFile(String name) {
return new ResContainer(name, new ArrayList<>());
Expand All @@ -79,6 +94,11 @@ public String getFileName() {
public CodeWriter getContent() {
return content;
}

@Nullable
public InputStream getBinary() {
return binary;
}

public void setContent(@Nullable CodeWriter content) {
this.content = content;
Expand Down
21 changes: 18 additions & 3 deletions jadx-core/src/main/java/jadx/core/xmlgen/ResourcesSaver.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.mockito.internal.util.io.IOUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -30,9 +34,6 @@ public ResourcesSaver(File outDir, ResourceFile resourceFile) {

@Override
public void run() {
if (!ResourceType.isSupportedForUnpack(resourceFile.getType())) {
return;
}
ResContainer rc = resourceFile.loadContent();
if (rc != null) {
saveResources(rc);
Expand Down Expand Up @@ -89,6 +90,20 @@ private void saveToFile(ResContainer rc, File outFile) {
cw.save(outFile);
return;
}
InputStream binary = rc.getBinary();
if(binary != null) {
try {
outFile.getParentFile().mkdirs();
FileOutputStream binaryFileStream = new FileOutputStream(outFile);
IOUtils.copy(binary, binaryFileStream);
binaryFileStream.close();
binary.close();
}
catch(IOException e) {
LOG.warn("Resource '{}' not saved, got exception {}", rc.getName(), e);
}
return;
}
LOG.warn("Resource '{}' not saved, unknown type", rc.getName());
}
}

0 comments on commit 8eef4a9

Please sign in to comment.