Skip to content

Commit

Permalink
Fix export always writing back as zip containers, even when workspace…
Browse files Browse the repository at this point in the history
… is loaded from a single regular file
  • Loading branch information
Col-E committed Jan 13, 2025
1 parent 9bc0850 commit 14d4b4c
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,22 @@

import jakarta.annotation.Nonnull;
import software.coley.collections.Unchecked;
import software.coley.recaf.info.*;
import software.coley.recaf.info.properties.builtin.*;
import software.coley.recaf.info.ClassInfo;
import software.coley.recaf.info.FileInfo;
import software.coley.recaf.info.Info;
import software.coley.recaf.info.JarFileInfo;
import software.coley.recaf.info.JvmClassInfo;
import software.coley.recaf.info.ZipFileInfo;
import software.coley.recaf.info.properties.builtin.PathOriginalNameProperty;
import software.coley.recaf.info.properties.builtin.PathPrefixProperty;
import software.coley.recaf.info.properties.builtin.PathSuffixProperty;
import software.coley.recaf.info.properties.builtin.ZipAccessTimeProperty;
import software.coley.recaf.info.properties.builtin.ZipCommentProperty;
import software.coley.recaf.info.properties.builtin.ZipCompressionProperty;
import software.coley.recaf.info.properties.builtin.ZipCreationTimeProperty;
import software.coley.recaf.info.properties.builtin.ZipModificationTimeProperty;
import software.coley.recaf.info.properties.builtin.ZipPrefixDataProperty;
import software.coley.recaf.util.ByteHeaderUtil;
import software.coley.recaf.util.ZipCreationUtils;
import software.coley.recaf.workspace.model.Workspace;
import software.coley.recaf.workspace.model.bundle.AndroidClassBundle;
Expand Down Expand Up @@ -103,11 +117,25 @@ public void export(@Nonnull Workspace workspace) throws IOException {
populate(workspace);
switch (outputType) {
case FILE:
// Test if we're supposed to just write the file as-is instead of bundling it in an archive.
// - Must only have one thing to write
// - Workspace input must be a single non-archive file
if (contents.size() == 1 &&
workspace.getPrimaryResource() instanceof WorkspaceFileResource primaryFileResource &&
!(primaryFileResource.getFileInfo() instanceof ZipFileInfo)) {
byte[] data = contents.values().iterator().next();
if (prefix != null)
consumer.write(prefix);
consumer.write(data);
return;
}

// Otherwise, lets make an archive.
ZipCreationUtils.ZipBuilder zipBuilder = ZipCreationUtils.builder();
if (createZipDirEntries)
zipBuilder = zipBuilder.createDirectories();

// Final copy for lambda, write all contents to ZIP buffer
// Final copy for lambda, write all contents to ZIP buffer.
ZipCreationUtils.ZipBuilder finalZipBuilder = zipBuilder;
contents.forEach((name, content) -> {
// Cannot mirror exact compression type, so we'll just do binary "is this compressed or nah?"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,25 @@ void testArbitraryHeaderDataIsKeptAfterExport() throws IOException {
assertEquals("SomethingElse.bin", outHeadName, "Mismatch in outputs expected first ZIP entry name");
assertEquals(zipHeadName, outHeadName, "Mismatch where normal ZIP entry for 'SomethingElse.bin' is supposed to be");
}

@Test
void testNonArchiveExportedAsIsAndNotBundledInAZipContainer() throws IOException {
// Make a basic [0, 1, 2, 3... 100]
byte[] bytes = new byte[100];
for (int i = 0; i < bytes.length; i++)
bytes[i] = (byte) i;

// Import the data.
WorkspaceResource resource = importer.importResource(ByteSources.wrap(bytes));
BasicWorkspace workspace = new BasicWorkspace(resource);

// Export it as a file.
ByteArrayWorkspaceExportConsumer bytesExport = new ByteArrayWorkspaceExportConsumer();
new WorkspaceExportOptions(WorkspaceOutputType.FILE, bytesExport).create().export(workspace);
byte[] output = bytesExport.getOutput();

// Verify the contents are the exact same. Because it's not an archive we should not be
// exporting it back bundled inside an archive.
assertArrayEquals(bytes, output, "Expected input and output to be exact match");
}
}

0 comments on commit 14d4b4c

Please sign in to comment.