当我解压缩文件时,文件名就像电影\好莱坞蜘蛛侠。实际上,是一个文件夹,是电影中的文件夹,蜘蛛侠是好莱坞的文件。
发布于 2017-04-28 05:39:57
如果电影\好莱坞\蜘蛛侠在创建压缩时是一个文件,那么它应该作为文件提取,不管它是否有扩展名(比如*.mp4,*.flv)。
您可以在名称空间java.util.zip下依赖java,文档链接是这里。
编写了一些只提取压缩文件的代码,它应该将文件条目提取为文件(不支持gzip,rar )。
private boolean extractFolder(File destination, File zipFile) throws ZipException, IOException
{
int BUFFER = 8192;
File file = zipFile;
//This can throw ZipException if file is not valid zip archive
ZipFile zip = new ZipFile(file);
String newPath = destination.getAbsolutePath() + File.separator + FilenameUtils.removeExtension(zipFile.getName());
//Create destination directory
new File(newPath).mkdir();
Enumeration zipFileEntries = zip.entries();
//Iterate overall zip file entries
while (zipFileEntries.hasMoreElements())
{
ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
String currentEntry = entry.getName();
File destFile = new File(newPath, currentEntry);
File destinationParent = destFile.getParentFile();
//If entry is directory create sub directory on file system
destinationParent.mkdirs();
if (!entry.isDirectory())
{
//Copy over data into destination file
BufferedInputStream is = new BufferedInputStream(zip
.getInputStream(entry));
int currentByte;
byte data[] = new byte[BUFFER];
//orthodox way of copying file data using streams
FileOutputStream fos = new FileOutputStream(destFile);
BufferedOutputStream dest = new BufferedOutputStream(fos, BUFFER);
while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, currentByte);
}
dest.flush();
dest.close();
is.close();
}
}
return true;//some error codes etc.
}该例程不执行任何异常处理,请在驱动程序代码中捕获ZipException和IOException。
https://stackoverflow.com/questions/43672241
复制相似问题