云文件系统初始化
This commit is contained in:
@@ -499,4 +499,75 @@ public class FileService {
|
||||
file.setFolderId(targetFolderId);
|
||||
fileMapper.updateById(file);
|
||||
}
|
||||
|
||||
public byte[] createZipArchive(List<Long> fileIds, Long userId) throws IOException {
|
||||
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
|
||||
java.util.zip.ZipOutputStream zos = new java.util.zip.ZipOutputStream(baos);
|
||||
|
||||
for (Long fileId : fileIds) {
|
||||
FileEntity file = fileMapper.selectById(fileId);
|
||||
if (file == null || !file.getUserId().equals(userId) || file.getIsDeleted() == 1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (file.getIsFolder() == 1) {
|
||||
// 递归添加文件夹内容
|
||||
addFolderToZip(zos, file, "", userId);
|
||||
} else {
|
||||
// 添加单个文件
|
||||
addFileToZip(zos, file, "");
|
||||
}
|
||||
}
|
||||
|
||||
zos.close();
|
||||
return baos.toByteArray();
|
||||
}
|
||||
|
||||
private void addFolderToZip(java.util.zip.ZipOutputStream zos, FileEntity folder, String parentPath, Long userId) throws IOException {
|
||||
String folderPath = parentPath + folder.getName() + "/";
|
||||
|
||||
// 获取文件夹下的所有内容
|
||||
List<FileEntity> children = fileMapper.selectList(
|
||||
new LambdaQueryWrapper<FileEntity>()
|
||||
.eq(FileEntity::getUserId, userId)
|
||||
.eq(FileEntity::getFolderId, folder.getId())
|
||||
.eq(FileEntity::getIsDeleted, 0)
|
||||
);
|
||||
|
||||
for (FileEntity child : children) {
|
||||
if (child.getIsFolder() == 1) {
|
||||
addFolderToZip(zos, child, folderPath, userId);
|
||||
} else {
|
||||
addFileToZip(zos, child, folderPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void addFileToZip(java.util.zip.ZipOutputStream zos, FileEntity file, String parentPath) throws IOException {
|
||||
if (file.getPath() == null) return;
|
||||
|
||||
Path filePath = Paths.get(storagePath).toAbsolutePath().resolve("files").resolve(file.getPath());
|
||||
if (!Files.exists(filePath)) return;
|
||||
|
||||
String zipEntryName = parentPath + file.getName();
|
||||
java.util.zip.ZipEntry zipEntry = new java.util.zip.ZipEntry(zipEntryName);
|
||||
zos.putNextEntry(zipEntry);
|
||||
|
||||
byte[] fileBytes = Files.readAllBytes(filePath);
|
||||
zos.write(fileBytes);
|
||||
zos.closeEntry();
|
||||
}
|
||||
|
||||
public List<FileEntity> getMovableFolders(Long userId, List<Long> excludeIds) {
|
||||
LambdaQueryWrapper<FileEntity> wrapper = new LambdaQueryWrapper<>();
|
||||
wrapper.eq(FileEntity::getUserId, userId)
|
||||
.eq(FileEntity::getIsDeleted, 0)
|
||||
.eq(FileEntity::getIsFolder, 1);
|
||||
|
||||
if (excludeIds != null && !excludeIds.isEmpty()) {
|
||||
wrapper.notIn(FileEntity::getId, excludeIds);
|
||||
}
|
||||
|
||||
return fileMapper.selectList(wrapper);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user