gpt4 book ai didi

java - 将文件夹压缩成 ZipFile

转载 作者:行者123 更新时间:2023-11-30 11:29:23 27 4
gpt4 key购买 nike

我有这个 Java 方法来上传文件。我试图通过将该文件夹压缩成一个 zip 文件并上传它来迎合尝试上传文件夹的用户。出于某种原因,就我而言 file.isDirectory()file.isFile()无法正常工作.. 即使文件名不包含任何扩展名,file.isFile()正在返回 true 和 isDirectory()返回假。还有 directory.list()返回 null 也很奇怪。

可能是什么问题?我做错了什么吗?

public File uploadFile(FileItem item, String filename, int ticket_id) throws IOException
{
FileOutputStream out = null;
InputStream fileContent = null;
File file = null;

try
{
//fullpath returns C://MyDocuments//zerafbe//Documents//apache-tomcat-7.0.29//webapps//attachments//t50\test

StringBuffer fullPath = new StringBuffer();
fullPath.append(Attachment.attachments_path);
fullPath.append("t");
fullPath.append(Integer.toString(ticket_id));
fullPath.append(File.separator);
fullPath.append(filename);

System.out.println("filename " + filename);

file = new File(fullPath.toString());

if (!file.exists())
{
// if directory does not exist, create it
file.getParentFile().mkdirs();
}

if (file.isFile())
{
// if file is not a folder
out = new FileOutputStream(file);
fileContent = item.getInputStream();

int read = 0;
final byte[] bytes = new byte[1024];

// read all the file and write it to created file
while ((read = fileContent.read(bytes)) != -1)
{
out.write(bytes, 0, read);
}
}
else if (file.isDirectory())
{
ZipFile appZip = new ZipFile(fullPath.toString());
appZip.generateFileList(file);
appZip.zipIt(filename + ".zip");
}
}
catch (FileNotFoundException e)
{
LogFile.logError("[FileUpload.uploadFile()] " + e.getMessage());
}
catch (IOException e1)
{
LogFile.logError("[FileUpload.uploadFile()] " + e1.getMessage());
}
finally
{
if (out != null)
{
out.close();
}

if (fileContent != null)
{
fileContent.close();
}
}

return file;
}

这是我正在使用的 ZipFile 类

public class ZipFile 
{
List<String> fileList = null;
String source_folder = "";

public ZipFile(String source_folder)
{
fileList = new ArrayList<String>();
this.source_folder = source_folder;
}

public void zipIt(String zipFile)
{
byte[] buffer = new byte[1024];
String source = "";

try
{
try
{
source = source_folder.substring(source_folder.lastIndexOf("\\") + 1, source_folder.length());
}
catch(Exception e)
{
source = source_folder;
}

FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(fos);

for (String file : this.fileList)
{
ZipEntry ze = new ZipEntry(source + File.separator + file);
zos.putNextEntry(ze);

FileInputStream in = new FileInputStream(source_folder + File.separator + file);

int len;
while ((len = in.read(buffer)) > 0)
{
zos.write(buffer, 0, len);
}

in.close();
}

zos.closeEntry();
//remember close it
zos.close();
}
catch(IOException ex)
{
ex.printStackTrace();
}
}

public void generateFileList(File node)
{
// add file only
if(node.isFile())
{
fileList.add(generateZipEntry(node.toString()));
}

if(node.isDirectory())
{
String[] subNode = node.list();

if (subNode != null) {

for(String filename : subNode)
{
generateFileList(new File (node, filename));
}
}
}
}

private String generateZipEntry(String path)
{
return path.substring(source_folder.length() + 1, path.length());
}
}

file.list()正在 generateFileList 中完成ZipFile 中的方法类(class)。我知道这会返回 null,因为我尝试使用 filename.indexOf(".") 检测文件是文件夹还是文件。而不是 isDirectory()isFile()因为他们没有工作。但我希望对此有一个解释。

感谢您的帮助!

最佳答案

if (!file.exists()) {
// if directory does not exist, create it
file.mkdirs();
}

将创建目录并测试 file.isDirectory() 将返回 true

关于java - 将文件夹压缩成 ZipFile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18376392/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com