1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.internal
.tmf
.ui
.project
.wizards
.importtrace
;
12 import java
.util
.zip
.ZipEntry
;
13 import java
.util
.zip
.ZipFile
;
15 import org
.eclipse
.ui
.internal
.wizards
.datatransfer
.ZipLeveledStructureProvider
;
18 * A Zip structure provider that makes sure to return safe paths. For example,
19 * if a Zip entry contains a ':' and is extracted on Windows, it will be changed
22 @SuppressWarnings("restriction")
23 public class SafePathZipLeveledStructureProvider
extends ZipLeveledStructureProvider
{
26 * Creates a provider which will operate on the passed Zip file.
29 * The source file to create the provider around
31 public SafePathZipLeveledStructureProvider(ZipFile sourceFile
) {
36 public String
getFullPath(Object element
) {
37 String name
= ((ZipEntry
) element
).getName();
38 return ArchiveUtil
.toValidNamesPath(name
).toOSString();
42 public String
getLabel(Object element
) {
43 if (element
.equals(getRoot())) {
44 return ((ZipEntry
) element
).getName();
46 String name
= ((ZipEntry
) element
).getName();
47 return stripPath(ArchiveUtil
.toValidNamesPath(name
).lastSegment());
51 * Strip the leading directories from the path. Copied from
52 * {@link ZipLeveledStructureProvider}
54 private String
stripPath(String path
) {
55 String strippedPath
= path
;
56 String pathOrig
= new String(strippedPath
);
57 for (int i
= 0; i
< getStrip(); i
++) {
58 int firstSep
= strippedPath
.indexOf('/');
59 // If the first character was a separator we must strip to the next
62 strippedPath
= strippedPath
.substring(1);
63 firstSep
= strippedPath
.indexOf('/');
65 // No separator was present so we're in a higher directory right
70 strippedPath
= strippedPath
.substring(firstSep
);