DREAMFIRE Docs ← Back to site
Loading...
Searching...
No Matches
DreamDir.java
Go to the documentation of this file.
1/*
2 * MIT License
3 *
4 * Copyright (c) 2025 Dreamfire Studio
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24package com.dreamfirestudios.dreamcore.DreamFile;
25
26import java.io.*;
27import java.nio.file.*;
28import java.util.ArrayList;
29import java.util.Comparator;
30import java.util.List;
31import java.util.Objects;
32import java.util.zip.*;
33
34public class DreamDir {
35
39 public static void deleteAllFiles(File directory, boolean cascadeFolders) {
40 if (!directory.exists() || !directory.isDirectory()) return;
41
42 for (File file : Objects.requireNonNull(directory.listFiles())) {
43 if (file.isDirectory() && cascadeFolders) {
44 deleteAllFiles(file, true);
45 file.delete(); // Delete the directory after its contents
46 } else {
47 file.delete();
48 }
49 }
50 }
51
55 public static void deleteDirectory(File directory) {
56 if (directory.exists() && directory.isDirectory()) {
57 deleteAllFiles(directory, true);
58 directory.delete();
59 }
60 }
61
65 public static void createDirectory(File directory) {
66 if (!directory.exists()) {
67 directory.mkdirs();
68 }
69 }
70
74 public static List<File> returnAllFilesFromDirectory(File directory, boolean cascadeFolders) {
75 List<File> fileList = new ArrayList<>();
76 if (!directory.exists() || !directory.isDirectory()) return fileList;
77
78 for (File file : Objects.requireNonNull(directory.listFiles())) {
79 if (file.isDirectory() && cascadeFolders) {
80 fileList.addAll(returnAllFilesFromDirectory(file, true));
81 } else {
82 fileList.add(file);
83 }
84 }
85 return fileList;
86 }
87
91 public static void CopyAllFiles(File dirA, File dirB, ArrayList<String> ignore){
92 try {
93 if(!ignore.contains(dirA.getName())) {
94 if(dirA.isDirectory()) {
95 if(!dirB.exists())
96 dirB.mkdirs();
97 String files[] = dirA.list();
98 for (String file : files) {
99 File srcFile = new File(dirA, file);
100 File destFile = new File(dirB, file);
101 CopyAllFiles(srcFile, destFile, ignore);
102 }
103 } else {
104 InputStream in = new FileInputStream(dirA);
105 OutputStream out = new FileOutputStream(dirB);
106 byte[] buffer = new byte[1024];
107 int length;
108 while ((length = in.read(buffer)) > 0)
109 out.write(buffer, 0, length);
110 in.close();
111 out.close();
112 }
113 }
114 } catch (IOException e) { }
115 }
116
117 public static long calculateTotalSize(File directory) {
118 if (!directory.exists()) return 0;
119
120 long totalSize = 0;
121 for (File file : Objects.requireNonNull(directory.listFiles())) {
122 if (file.isDirectory()) {
123 totalSize += calculateTotalSize(file);
124 } else {
125 totalSize += file.length();
126 }
127 }
128 return totalSize;
129 }
130
131 public static void moveAllFiles(File srcDir, File destDir, List<String> ignore) {
132 if (!srcDir.exists()) return;
133
134 createDirectory(destDir); // Ensure the destination directory exists
135
136 for (File file : Objects.requireNonNull(srcDir.listFiles())) {
137 if (ignore.contains(file.getName())) continue;
138
139 File destFile = new File(destDir, file.getName());
140 if (file.isDirectory()) {
141 moveAllFiles(file, destFile, ignore);
142 file.delete(); // Delete empty source directory
143 } else {
144 if (file.renameTo(destFile)) {
145 System.out.println("Moved file: " + file.getName());
146 } else {
147 System.err.println("Failed to move file: " + file.getName());
148 }
149 }
150 }
151 }
152
153 public static List<File> searchFiles(File directory, String query, boolean cascadeFolders) {
154 List<File> result = new ArrayList<>();
155 if (!directory.exists() || !directory.isDirectory()) return result;
156
157 for (File file : Objects.requireNonNull(directory.listFiles())) {
158 if (file.isDirectory() && cascadeFolders) {
159 result.addAll(searchFiles(file, query, true));
160 } else if (file.getName().contains(query)) {
161 result.add(file);
162 }
163 }
164 return result;
165 }
166
167 public static boolean compareDirectories(File dirA, File dirB) {
168 if (!dirA.exists() || !dirB.exists()) return false;
169
170 File[] filesA = dirA.listFiles();
171 File[] filesB = dirB.listFiles();
172
173 if (filesA == null || filesB == null || filesA.length != filesB.length) return false;
174
175 for (int i = 0; i < filesA.length; i++) {
176 File fileA = filesA[i];
177 File fileB = new File(dirB, fileA.getName());
178
179 if (fileA.isDirectory()) {
180 if (!compareDirectories(fileA, fileB)) return false;
181 } else if (!fileB.exists() || fileA.length() != fileB.length()) {
182 return false;
183 }
184 }
185 return true;
186 }
187
188 public static void zipDirectory(File directory, File zipFile) throws IOException {
189 try (FileOutputStream fos = new FileOutputStream(zipFile);
190 ZipOutputStream zos = new ZipOutputStream(fos)) {
191
192 zipFile(directory, directory.getName(), zos);
193 }
194 }
195
196 public static void extractZip(File zipFile, File destDir) throws IOException {
197 if (!destDir.exists()) destDir.mkdirs();
198
199 try (FileInputStream fis = new FileInputStream(zipFile);
200 ZipInputStream zis = new ZipInputStream(fis)) {
201
202 ZipEntry zipEntry;
203 while ((zipEntry = zis.getNextEntry()) != null) {
204 File newFile = new File(destDir, zipEntry.getName());
205
206 if (zipEntry.isDirectory()) {
207 newFile.mkdirs();
208 } else {
209 try (FileOutputStream fos = new FileOutputStream(newFile)) {
210 byte[] buffer = new byte[1024];
211 int length;
212 while ((length = zis.read(buffer)) > 0) {
213 fos.write(buffer, 0, length);
214 }
215 }
216 }
217 zis.closeEntry();
218 }
219 }
220 }
221
222 public static List<File> getFilesModifiedAfter(File directory, long timestamp, boolean cascadeFolders) {
223 List<File> files = new ArrayList<>();
224 if (!directory.exists() || !directory.isDirectory()) return files;
225
226 for (File file : Objects.requireNonNull(directory.listFiles())) {
227 if (file.isDirectory() && cascadeFolders) {
228 files.addAll(getFilesModifiedAfter(file, timestamp, true));
229 } else if (file.lastModified() > timestamp) {
230 files.add(file);
231 }
232 }
233 return files;
234 }
235
236 public static List<File> sortFiles(File directory, Comparator<File> comparator) {
237 List<File> files = new ArrayList<>(List.of(Objects.requireNonNull(directory.listFiles())));
238 files.sort(comparator);
239 return files;
240 }
241
242 // Example Comparators
243 public static Comparator<File> sortByName = Comparator.comparing(File::getName);
244 public static Comparator<File> sortBySize = Comparator.comparingLong(File::length);
245 public static Comparator<File> sortByLastModified = Comparator.comparingLong(File::lastModified);
246
247 private static void zipFile(File fileToZip, String fileName, ZipOutputStream zos) throws IOException {
248 if (fileToZip.isHidden()) return;
249
250 if (fileToZip.isDirectory()) {
251 if (fileName.endsWith("/")) {
252 zos.putNextEntry(new ZipEntry(fileName));
253 zos.closeEntry();
254 } else {
255 zos.putNextEntry(new ZipEntry(fileName + "/"));
256 zos.closeEntry();
257 }
258 for (File childFile : Objects.requireNonNull(fileToZip.listFiles())) {
259 zipFile(childFile, fileName + "/" + childFile.getName(), zos);
260 }
261 return;
262 }
263
264 try (FileInputStream fis = new FileInputStream(fileToZip)) {
265 ZipEntry zipEntry = new ZipEntry(fileName);
266 zos.putNextEntry(zipEntry);
267
268 byte[] buffer = new byte[1024];
269 int length;
270 while ((length = fis.read(buffer)) >= 0) {
271 zos.write(buffer, 0, length);
272 }
273 }
274 }
275}
static void moveAllFiles(File srcDir, File destDir, List< String > ignore)
static void createDirectory(File directory)
Creates the specified directory if it does not exist.
Definition DreamDir.java:65
static List< File > searchFiles(File directory, String query, boolean cascadeFolders)
static void extractZip(File zipFile, File destDir)
static long calculateTotalSize(File directory)
static void deleteAllFiles(File directory, boolean cascadeFolders)
Deletes all files in the specified directory.
Definition DreamDir.java:39
static void CopyAllFiles(File dirA, File dirB, ArrayList< String > ignore)
Copies all files from one directory to another, respecting the ignore list.
Definition DreamDir.java:91
static void zipDirectory(File directory, File zipFile)
static void deleteDirectory(File directory)
Deletes the specified directory if it exists.
Definition DreamDir.java:55
static boolean compareDirectories(File dirA, File dirB)
static List< File > getFilesModifiedAfter(File directory, long timestamp, boolean cascadeFolders)
static List< File > sortFiles(File directory, Comparator< File > comparator)
static List< File > returnAllFilesFromDirectory(File directory, boolean cascadeFolders)
Returns a list of all files in the specified directory.
Definition DreamDir.java:74