-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathFileUtils.java
163 lines (137 loc) · 5.86 KB
/
FileUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* Copyright © 2002 Instituto Superior Técnico
*
* This file is part of FenixEdu Academic.
*
* FenixEdu Academic is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FenixEdu Academic is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with FenixEdu Academic. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* @(#)FileUtils.java Created on Nov 5, 2004
*
*/
package org.fenixedu.academic.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import org.fenixedu.academic.domain.exceptions.DomainException;
import com.google.common.io.ByteStreams;
import com.google.common.io.Files;
/**
* @author Luis Cruz
* @author Shezad Anavarali
*
*/
@Deprecated
public class FileUtils {
// Cluster safe global unique temporary filename
private static final String TEMPORARY_FILE_GLOBAL_UNIQUE_NAME_PREFIX = UUID.randomUUID().toString();
private static final char[] SEPARATOR_CHARS = new char[] { '\\', '/' };
public static String getTemporaryFileBaseName() {
return TEMPORARY_FILE_GLOBAL_UNIQUE_NAME_PREFIX;
}
public static File createTemporaryFile() throws IOException {
final File temporaryFile = File.createTempFile(TEMPORARY_FILE_GLOBAL_UNIQUE_NAME_PREFIX, "");
// In case anything fails the file will be cleaned when jvm
// shutsdown
temporaryFile.deleteOnExit();
return temporaryFile;
}
public static File copyToTemporaryFile(final InputStream inputStream) throws IOException {
final File temporaryFile = createTemporaryFile();
FileOutputStream targetFileOutputStream = null;
try {
targetFileOutputStream = new FileOutputStream(temporaryFile);
ByteStreams.copy(inputStream, targetFileOutputStream);
} finally {
if (targetFileOutputStream != null) {
targetFileOutputStream.close();
}
inputStream.close();
}
return temporaryFile;
}
public static String getFilenameOnly(final String filename) {
for (final char separatorChar : SEPARATOR_CHARS) {
if (filename.lastIndexOf(separatorChar) != -1) {
return filename.substring(filename.lastIndexOf(separatorChar) + 1);
}
}
return filename;
}
public static String cleanupUserInputFilename(final String filename) {
return getFilenameOnly(filename);
}
public static String cleanupUserInputFileDisplayName(final String displayName) {
String filenamePart = getFilenameOnly(displayName);
String validChars = "_\\- .,:;!()*$&'=@";
if (!Pattern.matches("[\\p{IsLatin}0-9" + validChars + "]+", filenamePart)) {
throw new DomainException("errors.file.displayName.invalid.characters", validChars.replace("\\", ""));
}
return filenamePart;
}
public static File unzipFile(File file) throws IOException {
File tempDir = Files.createTempDir();
FileUtils.copyFileToAnotherDirWithRelativePaths(file.getParentFile(), tempDir, file);
try (ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(file))) {
ZipEntry zipEntry = zipInputStream.getNextEntry();
File zipContentFile = null;
File zipContentFileParentDir = null;
while (zipEntry != null) {
zipEntry.getName();
zipContentFile = new File(tempDir, zipEntry.getName());
zipContentFileParentDir = zipContentFile.getParentFile();
zipContentFileParentDir.mkdirs();
if (!zipEntry.isDirectory()) {
zipContentFile.createNewFile();
} else {
zipContentFile.mkdirs();
}
zipContentFile.deleteOnExit();
if (!zipEntry.isDirectory() && zipContentFile.exists() && zipContentFile.canWrite()) {
try (OutputStream zipOs = new FileOutputStream(zipContentFile)) {
ByteStreams.copy(zipInputStream, zipOs);
}
}
zipInputStream.closeEntry();
zipEntry = zipInputStream.getNextEntry();
}
}
return tempDir;
}
public static String makeRelativePath(String absoluteParentPath, String originalAbsoluteFilePath, String uniqueId) {
if (originalAbsoluteFilePath != null && absoluteParentPath != null
&& originalAbsoluteFilePath.length() > absoluteParentPath.length()) {
return originalAbsoluteFilePath.substring(absoluteParentPath.length() + 1);
} else {
return uniqueId;
}
}
public static File copyFileToAnotherDirWithRelativePaths(File srcDir, File destDir, File originalFile)
throws FileNotFoundException, IOException {
String relativePath = makeRelativePath(srcDir.getAbsolutePath(), originalFile.getAbsolutePath(), "");
File newFile = new File(destDir, relativePath);
try (FileInputStream fis = new FileInputStream(originalFile); FileOutputStream fos = new FileOutputStream(newFile);) {
ByteStreams.copy(fis, fos);
return newFile;
}
}
}