From 349be6fa7875b70b9b296e50e7803ed4b6b02312 Mon Sep 17 00:00:00 2001 From: cdeptula Date: Sat, 20 Feb 2016 15:22:49 -0700 Subject: [PATCH] [SPARKL-166] Fix ZipUtil so zips created are readale in Windows --- .../src/pt/webdetails/cpk/utils/ZipUtil.java | 11 ++++--- .../pt/webdetails/cpk/utils/ZipUtilTest.java | 32 +++++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 cpk-core/test-src/pt/webdetails/cpk/utils/ZipUtilTest.java diff --git a/cpk-core/src/pt/webdetails/cpk/utils/ZipUtil.java b/cpk-core/src/pt/webdetails/cpk/utils/ZipUtil.java index 8166493d0..ca347d002 100644 --- a/cpk-core/src/pt/webdetails/cpk/utils/ZipUtil.java +++ b/cpk-core/src/pt/webdetails/cpk/utils/ZipUtil.java @@ -40,7 +40,7 @@ public class ZipUtil { private String zipName; private FileInputStream fis; - private FileName topFilename; + protected FileName topFilename; ArrayList fileListing = new ArrayList(); protected Log logger = LogFactory.getLog( this.getClass() ); @@ -107,7 +107,8 @@ private ZipOutputStream writeEntriesToZip( Collection files, ZipOutp logger.debug( "Files to process:" + files.size() ); logger.debug( "Files processed: " + i ); logger.debug( "Files remaining: " + ( files.size() - i ) ); - logger.debug( file.getName().getPath() ); + logger.debug( "Processing file: " + file.getName().getPath() ); + logger.debug( "File path in zip: " + removeTopFilenamePathFromString( file.getName().getPath() ) ); fileListing.add( removeTopFilenamePathFromString( file.getName().getPath() ) ); @@ -229,10 +230,12 @@ private FileName getTopFileName( List files ) { return topFileName; } - private String removeTopFilenamePathFromString( String path ) { + protected String removeTopFilenamePathFromString( String path ) { String filteredPath = null; - int index = this.topFilename.getParent().getPath().length(); + //[SPARKL-166] Add +1 to the index so the filteredPath does not start with /. Starting with / makes + // the zip unreadable in the Windows zip utility. + int index = this.topFilename.getParent().getPath().length() + 1; filteredPath = path.substring( index ); diff --git a/cpk-core/test-src/pt/webdetails/cpk/utils/ZipUtilTest.java b/cpk-core/test-src/pt/webdetails/cpk/utils/ZipUtilTest.java new file mode 100644 index 000000000..31c47294d --- /dev/null +++ b/cpk-core/test-src/pt/webdetails/cpk/utils/ZipUtilTest.java @@ -0,0 +1,32 @@ +package pt.webdetails.cpk.utils; + + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.pentaho.di.core.vfs.KettleVFS; +import org.pentaho.di.trans.steps.systemdata.SystemData; + + +/** + * Created by chrisdeptula on 2/20/16. + */ +public class ZipUtilTest { + + private static ZipUtil zipUtil; + + @Before + public void setUp() { + zipUtil = new ZipUtil(); + } + + @Test + public void testRemoveTopFilenamePathFromString() throws Exception { + String topFileName = "file:///pentaho/biserver/pentaho-solutions/system/sparkl/test.txt"; + String fileName = "/pentaho/biserver/pentaho-solutions/system/sparkl/dashboards/admin/Main.cdfde"; + zipUtil.topFilename = KettleVFS.getFileObject(topFileName).getParent().getName(); + String removeTopFilename = zipUtil.removeTopFilenamePathFromString(fileName); + Assert.assertEquals( "sparkl/dashboards/admin/Main.cdfde", removeTopFilename ); + } + +}