Skip to content

Commit 1d25300

Browse files
Fixes to writing non-tabular resources to zip-compressed packages
1 parent fe858cd commit 1d25300

File tree

3 files changed

+131
-8
lines changed

3 files changed

+131
-8
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>io.frictionlessdata</groupId>
66
<artifactId>datapackage-java</artifactId>
7-
<version>0.9.5-SNAPSHOT</version>
7+
<version>0.9.6-SNAPSHOT</version>
88
<packaging>jar</packaging>
99
<issueManagement>
1010
<url>https://github.com/frictionlessdata/datapackage-java/issues</url>
@@ -23,7 +23,7 @@
2323
<maven.compiler.source>${java.version}</maven.compiler.source>
2424
<maven.compiler.target>${java.version}</maven.compiler.target>
2525
<maven.compiler.compiler>${java.version}</maven.compiler.compiler>
26-
<tableschema-java-version>0.9.5</tableschema-java-version>
26+
<tableschema-java-version>0.9.6</tableschema-java-version>
2727
<junit.version>5.13.2</junit.version>
2828
<slf4j-simple.version>2.0.17</slf4j-simple.version>
2929
<apache-commons-collections4.version>4.5.0</apache-commons-collections4.version>

src/main/java/io/frictionlessdata/datapackage/resource/AbstractResource.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,11 @@
3232
import org.apache.commons.csv.CSVFormat;
3333
import org.apache.commons.csv.CSVPrinter;
3434

35-
import java.io.FileOutputStream;
36-
import java.io.IOException;
37-
import java.io.Writer;
35+
import java.io.*;
3836
import java.net.URI;
3937
import java.net.URISyntaxException;
4038
import java.nio.charset.StandardCharsets;
39+
import java.nio.file.FileSystem;
4140
import java.nio.file.Files;
4241
import java.nio.file.Path;
4342
import java.util.*;
@@ -774,8 +773,9 @@ public void writeData(Path outputDir) throws Exception {
774773
String fileName = fName+"."+getSerializationFormat();
775774
Table t = tables.get(cnt++);
776775
Path p;
776+
FileSystem fileSystem = outputDir.getFileSystem();
777777
if (outputDir.toString().isEmpty()) {
778-
p = outputDir.getFileSystem().getPath(fileName);
778+
p = fileSystem.getPath(fileName);
779779
} else {
780780
p = outputDir.resolve(fileName);
781781
}
@@ -796,9 +796,15 @@ public void writeData(Path outputDir) throws Exception {
796796
} else {
797797
// if serializationFormat is not set (probably non-tabular data), serialize the data to a binary file
798798
byte [] data = (byte[])this.getRawData();
799-
try (FileOutputStream fos = new FileOutputStream(p.toFile())){
800-
fos.write(data);
799+
try (OutputStream out = Files.newOutputStream(p)) {
800+
out.write(data);
801801
}
802+
/*} else {
803+
try (FileOutputStream fos = new FileOutputStream(p.toFile())){
804+
fos.write(data);
805+
}
806+
}*/
807+
802808
}
803809
}
804810
}

src/test/java/io/frictionlessdata/datapackage/resource/NonTabularResourceTest.java

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.List;
2222
import java.util.Map;
2323
import java.util.Set;
24+
import java.util.ArrayList;
2425

2526
import static io.frictionlessdata.datapackage.Profile.PROFILE_DATA_PACKAGE_DEFAULT;
2627
import static io.frictionlessdata.datapackage.Profile.PROFILE_DATA_RESOURCE_DEFAULT;
@@ -235,6 +236,122 @@ public void testNonTabularResource3() throws Exception {
235236

236237
}
237238

239+
240+
@Test
241+
@DisplayName("Test creating ZIP-compressed datapackage with PDF as FilebasedResource")
242+
public void testZipCompressedDatapackageWithPdfResource() throws Exception {
243+
// Create temporary directories
244+
Path tempDirPath = Files.createTempDirectory("datapackage-source-");
245+
Path tempOutputPath = Files.createTempDirectory("datapackage-output-");
246+
247+
// Copy PDF to temporary directory
248+
byte[] pdfContent = TestUtil.getResourceContent("/fixtures/files/sample.pdf");
249+
File pdfFile = new File(tempDirPath.toFile(), "sample.pdf");
250+
Files.write(pdfFile.toPath(), pdfContent);
251+
252+
// Create FilebasedResource directly for the PDF
253+
FilebasedResource pdfResource = new FilebasedResource(
254+
"pdf-document",
255+
List.of(new File("sample.pdf")),
256+
tempDirPath.toFile()
257+
);
258+
259+
// Create a package with the resource
260+
List<Resource> resources = new ArrayList<>();
261+
resources.add(pdfResource);
262+
io.frictionlessdata.datapackage.Package pkg = new io.frictionlessdata.datapackage.Package(resources);
263+
pkg.setName("pdf-package");
264+
pkg.setProfile(Profile.PROFILE_DATA_PACKAGE_DEFAULT);
265+
266+
// Set default resource profile and format
267+
pdfResource.setProfile(Profile.PROFILE_DATA_RESOURCE_DEFAULT);
268+
pdfResource.setFormat("pdf");
269+
pdfResource.setTitle("Sample PDF Document");
270+
pdfResource.setDescription("A test PDF file");
271+
pdfResource.setMediaType("application/pdf");
272+
pdfResource.setEncoding("UTF-8");
273+
274+
// Write as ZIP-compressed package
275+
File zipFile = new File(tempOutputPath.toFile(), "datapackage.zip");
276+
pkg.write(zipFile, true); // true for compression
277+
278+
// Verify ZIP file was created
279+
Assertions.assertTrue(zipFile.exists());
280+
Assertions.assertTrue(zipFile.length() > 0);
281+
282+
// Read the package back from ZIP
283+
io.frictionlessdata.datapackage.Package readPackage = new io.frictionlessdata.datapackage.Package(zipFile.toPath(), true);
284+
285+
// Verify package properties
286+
Assertions.assertEquals("pdf-package", readPackage.getName());
287+
Assertions.assertEquals(1, readPackage.getResources().size());
288+
289+
// Verify PDF resource
290+
Resource<?> readResource = readPackage.getResource("pdf-document");
291+
Assertions.assertNotNull(readResource);
292+
Assertions.assertEquals(Profile.PROFILE_DATA_RESOURCE_DEFAULT, readResource.getProfile());
293+
Assertions.assertEquals("pdf", readResource.getFormat());
294+
Assertions.assertEquals("Sample PDF Document", readResource.getTitle());
295+
Assertions.assertEquals("application/pdf", readResource.getMediaType());
296+
}
297+
298+
@Test
299+
@DisplayName("Test creating uncompressed datapackage with PDF as FilebasedResource")
300+
public void testUnCompressedDatapackageWithPdfResource() throws Exception {
301+
// Create temporary directories
302+
Path tempDirPath = Files.createTempDirectory("datapackage-source-");
303+
Path tempOutputPath = Files.createTempDirectory("datapackage-output-");
304+
305+
// Copy PDF to temporary directory
306+
byte[] pdfContent = TestUtil.getResourceContent("/fixtures/files/sample.pdf");
307+
File pdfFile = new File(tempDirPath.toFile(), "sample.pdf");
308+
Files.write(pdfFile.toPath(), pdfContent);
309+
310+
// Create FilebasedResource directly for the PDF
311+
FilebasedResource pdfResource = new FilebasedResource(
312+
"pdf-document",
313+
List.of(new File("sample.pdf")),
314+
tempDirPath.toFile()
315+
);
316+
317+
// Create a package with the resource
318+
List<Resource> resources = new ArrayList<>();
319+
resources.add(pdfResource);
320+
io.frictionlessdata.datapackage.Package pkg = new io.frictionlessdata.datapackage.Package(resources);
321+
pkg.setName("pdf-package");
322+
pkg.setProfile(Profile.PROFILE_DATA_PACKAGE_DEFAULT);
323+
324+
// Set default resource profile and format
325+
pdfResource.setProfile(Profile.PROFILE_DATA_RESOURCE_DEFAULT);
326+
pdfResource.setFormat("pdf");
327+
pdfResource.setTitle("Sample PDF Document");
328+
pdfResource.setDescription("A test PDF file");
329+
pdfResource.setMediaType("application/pdf");
330+
pdfResource.setEncoding("UTF-8");
331+
332+
// Write as ZIP-compressed package
333+
File packageFile = new File(tempOutputPath.toFile(), "datapackage");
334+
pkg.write(packageFile, false); // false for compression
335+
336+
Assertions.assertTrue(packageFile.exists());;
337+
338+
// Read the package back from ZIP
339+
io.frictionlessdata.datapackage.Package readPackage = new io.frictionlessdata.datapackage.Package(packageFile.toPath(), true);
340+
341+
// Verify package properties
342+
Assertions.assertEquals("pdf-package", readPackage.getName());
343+
Assertions.assertEquals(1, readPackage.getResources().size());
344+
345+
// Verify PDF resource
346+
Resource<?> readResource = readPackage.getResource("pdf-document");
347+
Assertions.assertNotNull(readResource);
348+
Assertions.assertEquals(Profile.PROFILE_DATA_RESOURCE_DEFAULT, readResource.getProfile());
349+
Assertions.assertEquals("pdf", readResource.getFormat());
350+
Assertions.assertEquals("Sample PDF Document", readResource.getTitle());
351+
Assertions.assertEquals("application/pdf", readResource.getMediaType());
352+
}
353+
354+
238355
/**
239356
* A non-tabular resource
240357
*/

0 commit comments

Comments
 (0)