百度云盘外链不支持云解压怎么办,百度网盘不支持外链云解压怎么办

注意:虽然文章中的一些代码片段看起来包含大量代码,但实际上只有少量代码有助于删除trycatch 并释放资源。关键是要仔细观察并效仿。

首先,创建一个类似的辅助视图类。

如果您想学习Java 工程、高性能和分发,我们将简要解释。对微服务、Spring、MyBatis、Netty源码分析感兴趣的朋友可以加入我的Java进阶交流:854630135。群里有阿里巴巴大牛的直播技术讲解,还有Java海量互联网技术视频。免费与大家分享。

1 public enum FileType { 2 //未知3 UNKNOWN, 4 //压缩文件5 ZIP, RAR, _7Z, TAR, GZ, TAR_GZ, BZ2, TAR_BZ2, 6 //位图文件7 BMP, PNG, JPG, JPEG, 8//矢量文件9 SVG, 10 //音视频文件11 AVI, MP4, MP3, AAR, OGG, WAV, WAVE12 } 该类主要用于集中管理各种文件类型。 当然,你也可以选择不写。这个类是的,可以直接用字符串来表示。

接下来,我还编写了一个方法来获取文件的实际类型。

为什么要写这个方法呢?这是非常危险的,因为你可以直接改变文件的后缀。

1 /** 2 * 获取文件的实际类型3 * 4 * @param file 要获取的文件类型。 5 * @return 文件类型枚举。 6 */7 private static FileType getFileType(File file){ 8 FileInputStream inputStream=null; 9 try{10 inputStream=new FileInputStream(file);11 byte[] head=new byte[4];12 if (-1==inputStream.read(head)) {13 return FileType.UNKNOWN;14 }15 int headHex=0;16 for (byte b : head) {17 headHex=8;18 headHex |=b;19 }20 switch (headHex) { 21 案例0x504B0304:22 返回FileType.ZIP; 23 案例0x776f7264:24 返回FileType.TAR; 27 案例0x425a6839:28 返回FileType.BZ2; f7f833 36030 返回FileType.GZ;31 案例0x52 61722 1:32 返回FileType.RAR ; 33 默认:34 返回FileType.UNKNOWN;35 }36 }catch (Exception e){37 e.printStackTrace();38 }最后{39 try {40 if(inputStream!=null){41 inputStream.close();42 } 43 } catch (IOException e) { 44 e. printStackTrace();45 }46 }47 return FileType.UNKNOWN;48 }这里我们使用文件的头信息来确定类型。这里不显示其他文件的头文件信息。如果需要,您可以运行该文件并检查headHex 的值。

最后,还有一个用于创建目录的辅助方法。

如果您想学习Java 工程、高性能和分发,我们将简要解释。对微服务、Spring、MyBatis、Netty源码分析感兴趣的朋友可以加入我的Java进阶交流:854630135。群里有阿里巴巴大牛的直播技术讲解,还有Java海量互联网技术视频。免费与大家分享。

1 /** 2 * 构建目录3 * @param OutputDir 输出目录4 * @param subDir 子目录5 */6 private static void createDirectory(String OutputDir, String subDir){ 7 File file=new File(outputDir);==null || subDir.trim().equals(\\\’\\\’))) {//子目录不为空9 file=new File(outputDir + File.separator + subDir);10 }11 if ( !file. contains()){12 if(!file.getParentFile().exists()){13 file.getParentFile().mkdirs();14 }15 file.mkdirs();16 }17 }tar文件解压

接下来是正宗的美食。第一个是如何解压缩tar 文件。

幸运的是,JDK 附带了一个解压tar 文件的工具。我们来看看下面的代码。

1 /** 2 * 解压tar文件3 * @param file 压缩包文件4 * @param targetPath 目标文件夹5 * @param delete 解压后是否删除原压缩包文件6 */7 private static void decompressTar(File file , String targetPath, boolean delete){ 8 FileInputStream fis=null; 9 OutputStream fos=null; 12 fis=new FileInputStream(file); 13 tarInputStream(fis, 1024 * 2);输出目录15 createDirectory(targetPath, null);16 17 TarEntryentry=null;18 while(true){19entry=tarInputStream.getNextEntry();20 if(entry==null){21break; 23 if(entry.isDirectory) ()){24 createDirectory(targetPath,entry.getName()) //创建子目录25 }else{26 fos=new FileOutputStream(new File(targetPath + File.separator +entry.getName); ()));27 int count;28 字节data[]=new byte[2048];29 while ((count=tarInputStream.read(data)) !=-1) {30 fos.write(data, 0, count );31 }32 fos .flush();33 }34 }35 } catch (IOException e) {36 e.printStackTrace();37 }最后{38 尝试{39 if(fis !=null){ 40 fis.close ();41 }42 if(fos !=null){43 fos.close();44 }45 if(tarInputStream !=null){46 tarInputStream.close();47 }48 } catch (IOException e) {49 e.printStackTrace(); 50 }51 }52 }注意方法参数是是否要删除原来的压缩包。如果需要去除,就应该去除。必须!必须!在流关闭之前无法删除它。除非您关闭它,否则它不会被删除。您还可以从方法调用者中删除此参数,以便不需要传递它。

解压bz2文件

要解压bz2 文件,请使用Apache 的commons.compress 工具进行解压。首先下载jar包commons-compress-1.9.jar(1.8好像有问题,所以改成了1.9)。

1 /** 2 * 解压bz2文件3 * @param file 压缩包文件4 * @param targetPath 目标文件夹5 * @param delete 解压后是否删除原压缩包文件6 */7 public static void decompressBZ2(File file , String targetPath, boolean delete){ 8 FileInputStream fis=null; 9 OutputStream fos=null;10 BZip2CompressorInputStream bis=null;11 String suffix=\\\’.bz2\\\’;12 try {13 fis=new FileInputStream (file); 14 bis=new BZip2CompressorInputStream(fis);15 //创建输出目录16 createDirectory(targetPath, null);17 File tempFile=new File(targetPath + File.separator + file.getName().replace (suffix, \\\’\\ \’ ) );18 fos=new FileOutputStream(tempFile);19 20 int count;21 byte data[]=new byte[2048];22 while ((count=bis.read(data)) !=-1) {23 fos . write(data, 0, count);24 }25 fos.flush();26 } catch (IOException e) {27 e.printStackTrace();28 } 最后{29 尝试{30 if(fis !=null) { 31 fis.close();32 }33 if(fos !=null){34 fos.close();35 }36 if(bis !=null){37 bis.close() ;38 }39 } catch ( IOException e ) {40 e.printStackTrace();41 }42 }43 }提取tar.bz2 文件

1 /** 2 * 解压tar.bz2文件3 * @param file 压缩包文件4 * @param targetPath 目标文件夹5 * @param delete 解压后是否删除原压缩包文件6 */7 public static void decompressTarBz2 (文件文件,字符串targetPath,布尔删除){ 8 FileInputStream fis=null; 10 BZip2CompressorInputStream bis=null; 12 try {13 fis=new FileInputStream(file); 15 tis=new TarInputStream( bis, 1024 * 2);16 //创建输出目录17 createDirectory(targetPath, null);18 TarEntry Entry;19 while((entry=tis.getNextEntry()) !=null){20 if(entry.isDirectory()){21 createDirectory (targetPath,entry.getName()); //创建子目录22 }else{23 fos=new FileOutputStream(new File(targetPath + File.separator +entry . getName()) );24 int count;25 字节数据[ ]=new byte[2048];26 while ((count=tis.read(data)) !=-1) {27 fos.write(data, 0 , count);28 }29 fos.flush();30 } 31 }32 } catch (IOException e) {33 e.printStackTrace();34 }最后{35 尝试{36 if(fis !=null){37 fis . close() ;38 }39 if(fos !=null) {40 fos.close();41 }42 if(bis !=null){43 bis.close();44 }45 if(tis !=null ) {46 tis .close();47 }48 } catch ( IOException e) {49 e.printStackTrace();50 }51 }52 }提取tar.gz 文件

如果您想学习Java 工程、高性能和分发,我们将简要解释。对微服务、Spring、MyBatis、Netty源码分析感兴趣的朋友可以加入我的Java进阶交流:854630135。群里有阿里巴巴大牛的直播技术讲解,还有Java海量互联网技术视频。免费与大家分享。

1 /** 2 * 解压tar.gz文件3 * @param file 压缩包文件4 * @param targetPath 目标文件夹5 * @param delete 解压后是否删除原压缩包文件6 */7 private static void decompressTarGz (文件文件,字符串targetPath,布尔删除){ 8 FileInputStream fileInputStream=null; 10 GZIPInputStream tarIn=null; 13 try {14 fileInputStream(file) );15bufferedInputStream=new BufferedInputStream(fileInputStream);16 gzipIn=new GZIPInputStream (bufferedInputStream); 17 tarIn=new TarInputStream(gzipIn, 1024 * 2);18 19 //创建输出目录20 createDirectory(targetPath, null);21 22 TarEntryEntry=null;23 while((entry=tarIn.getNextEntry ()) !=null) {24 if(entry.isDirectory()){ //这是一个目录25 createDirectory(targetPath,entry.getName()); //创建一个子目录26 }else{ //文件27 File tempFIle=new File(targetPath + File.separator +entry.getName());28 createDirectory(tempFIle.getParent() + File.separator, null);29 out=new FileOutputStream(tempFIle);30 int len=0;31 byte[] b=新字节[2048];32 33 while ((len=tarIn.read(b)) !=-1){34 out .write(b , 0, len);35 }36 out.flush();37 }38 }39 } catch (IOException e) {40 e.printStackTrace();41 }最后{42 try {43 if(out !=null){ 44 out.close();45 }46 if(tarIn !=null){47 tarIn .close();48 }49 if(gzipIn !=null){50 gzipIn.close();51 }52 if(bufferedInputStream !=null){53bufferedInputStream.close( );54 }55 if(fileInputStream !=null) {56 fileInputStream.close();57 }58 } catch (IOException e) {59 e.printStackTrace(); 60 }61 } 62}gz 解压文件

1 /** 2 * 解压gz文件3 * @param file 压缩包文件4 * @param targetPath 目标文件夹5 * @param delete 解压后是否删除原压缩包文件6 */7 private static void decompressGz(File file , String targetPath, boolean delete){ 8 FileInputStream fileInputStream=null; 9 GZIPInputStream gzipIn=null; 11 String suffix=\\\’.gz\\\’ 12 try {13 fileInputStream(file); 15 gzipIn=new GZIPInputStream (fileInputStream); //创建输出目录16 createDirectory(targetPath, null);17 18 File tempFile=new File(targetPath + File.separator + file.getName().replace(suffix, \\\’ \\\’ ) );19 out=new FileOutputStream (tempFile);20 int count;21 byte data[]=new byte[2048];22 while ((count=gzipIn.read(data)) !=-1) {23 out.write (data, 0, count) ;24 }25 out.flush();26 } catch (IOException e) {27 e.printStackTrace();28 }最后{29 try {30 if(out !=null) {31 out.close();32 } 33 if(gzipIn !=null){34 gzipIn.close();35 }36 if(fileInputStream !=null){37 fileInputStream.close();38 }39 } catch ( IOException e ) {40 e.printStackTrace() ;41 }42 }43 }7z 解压文件

1 /** 2 * 解压7z文件3 * @param file 压缩包文件4 * @param targetPath 目标文件夹5 * @param delete 解压后是否删除原压缩包文件6 */7 private static void decompress7Z (File file , String targetPath, boolean delete){ 8 SevenZFile SevenZFile=null; 9 OutputStream OutputStream=null;10 try {11 SevenZFile=new SevenZFile(file);12 //创建输出目录13 createDirectory(targetPath, null );14 SevenZArchiveEntry 条目; 15 16 while((entry=SevenZFile.getNextEntry()) !=null){17 if(entry.isDirectory()){18 createDirectory(targetPath,entry.getName()) //创建子目录创建19 }else{20 OutputStream=new FileOutputStream(new File(targetPath + File.separator +entry.getName()));21 int len=0;22 byte[] b=new byte[2048];23 while ((len=SevenZFile. read( b)) !=-1){24 OutputStream.write(b, 0, len);25 }26 OutputStream.flush();27 }28 }29 } catch (IOException e) { 30 e.printStackTrace() ;31 }最后{32 try {33 if(sevenZFile !=null){34 SevenZFile.close();35 }36 if(outputStream !=null){37 OutputStream.close();38 }39 } catch (IOException e ) { 40 e.printStackTrace();41 }42 }43 }rar 文件提取

首先下载jar包:junrar-0.7.jar、xz-1.5.jar、commons-logging.jar

如果您想学习Java 工程、高性能和分发,我们将简要解释。对微服务、Spring、MyBatis、Netty源码分析感兴趣的朋友可以加入我的Java进阶交流:854630135。群里有阿里巴巴大牛的直播技术讲解,还有Java海量互联网技术视频。免费与大家分享。

1 /** 2 * 解压RAR文件3 * @param file 压缩包文件4 * @param targetPath 目标文件夹5 * @param delete 解压后是否删除原压缩包文件6 */7 private static void decompressRAR(File file , String targetPath, boolean delete){ 8 Archive archive=null; 9 OutputStream OutputStream=null;10 try {11 archive=new Archive(file);12 FileHeader fileHeader;13 //创建输出目录14 createDirectory(targetPath, null ); 15 while( (fileHeader=archive.nextFileHeader()) !=null){16 if(fileHeader.isDirectory()){17 createDirectory(targetPath, fileHeader.getFileNameString().trim()) //创建子目录18 } else {19 OutputStream=new FileOutputStream(new File(targetPath + File.separator + fileHeader.getFileNameString().trim()));20 archive.extractFile(fileHeader, OutputStream);21 }22 }23 } catch (RarException | } IOException e ) {24 e.printStackTrace();25 }最后{26 尝试{27 if(archive !=null){28 archive.close();29 }30 if (outputStream !=null){31 OutputStream.close( ) ; 32 }33 } catch (IOException e) {34 e.printStackTrace();35 }36 }37 }需要更多“Java”信息的同学可以查看后台私信回复关键词‘666’获取优质信息。有关Java 架构的信息

本文和图片来自网络,不代表火豚游戏立场,如若侵权请联系我们删除:https://www.huotun.com/game/669447.html

(0)
上一篇 2024年6月4日
下一篇 2024年6月4日

相关推荐

  • 和平精英多久可以换一次系统?

    和平精英多久可以换一次系统? 和平精英在玩的时候通常可以采用半年更换一次系统的情况,及时更新游戏并且更换系统可以保持游戏使用的流畅性和相关文件的储存安全 和平精英换苹果系统里面还有好友么? 和平精英换苹果系统里面还有好友么? 和平精英是一款很好玩的手游类生存游戏软件 换了苹果系统里面的好友,它是存在的,只不过你里面的一些什么段位什么的,他都会轻灵的,甚至是一…

    游戏快讯 49分钟前
  • 和平精英身法衣服搭配平民?

    和平精英身法衣服搭配平民? 可以的。 身法衣服比如金币棉袄、身法裤、小白衣、海上生明月帽子等等搭配平民很合适。  其实这完全可以理解,因为该群体本身经济来源就比较少,再加上游戏内也有氪金数额的限制,所以只能选用一些免费或者价格较低的散件进行搭配了。  身法衣怎么搭配? 1、褐色搭配原则与白色搭配,给人一种清纯的感觉。 2、金褐色及膝圆裙与…

    游戏快讯 2小时前
  • 和平精英实名认证怎么改成成年人?

    和平精英实名认证怎么改成成年人? 实名认证是改不了,换账号吧。 和平精英怎么实名认证? 和平精英实名认证方法: 1.第一步我们首先需要知道和平精英实名认证的话,如果是qq登录就用qq进行实名认证,如果微信登录就用微信实名认证 2.第二步这里以qq登录为例,点击qq钱包->设置 3.第三步进去之后,点击实名认证 4.第四步进去之后,可以进行实名认证,根据…

    游戏快讯 6小时前
  • 刺激战场皮肤在和平精英里能用吗?

    刺激战场皮肤在和平精英里能用吗? 1. 不能用。2. 因为刺激战场和和平精英是两款不同的游戏,它们的游戏系统和皮肤设计都是独立的,所以刺激战场的皮肤无法在和平精英中使用。3. 尽管两款游戏都是吃鸡类游戏,但它们的开发公司和游戏平台不同,所以皮肤等游戏道具也是独立的。如果你想在和平精英中使用皮肤,需要在和平精英游戏内购买或获得相应的皮肤。 和平精英怎么重返战场…

    游戏快讯 7小时前
  • 和平精英如何获取永久套装?

    和平精英如何获取永久套装? 目前还不能获得永久版的,只有30天使用权。;和平精英;完美勇士套装是属于期限套装,以后会不会上架或者做活动放出来就不知道了。 和平精英永久买皮肤真的? 是真的,和平精英可以永久买皮肤,一次性购买,永久使用 免费领皮肤永久和平精英? 那也只是骗人的而已,连最起码的黑色伏地魔都要600个服饰币,可以免费领永久的皮肤都是假的,除非你有申…

    游戏快讯 9小时前