前言
如何用Java对文件进行加压和压缩
上篇文章说了项目中对根据URL提供的HTML代码中的文件URL进行下载,将下载后的文件存放在服务器上,但是文件下载下来都是ZIP压缩包。那么这篇就来看Java如何多文件进行解压缩操作。
一、正文
这里没有使用其他的jar包,利用Java中的IO流直接对文件进行操作,为了方便将文件放入桌面,路径为:C:\Users\Surpass\Desktop
。
二、使用步骤
博主尽量在代码中添加明确的注释,以便于理解,所以直接贴代码了。
1.单文件压缩
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
public class SingleZipCompression {
private static InputStream inputStream; private static ZipOutputStream zipOutputStream; private static OutputStream outputStream; private static String filePath = "C:\\Users\\Surpass\\Desktop\\Linux就该这么学 高清晰PDF.pdf"; public static void main(String[] args) { try { File file = new File(filePath); inputStream = new FileInputStream(file);
outputStream = new FileOutputStream(file.getParent()+"\\"+file.getName().substring(0,file.getName().lastIndexOf("."))+".zip"); zipOutputStream = new ZipOutputStream(outputStream);
zipOutputStream.putNextEntry(new ZipEntry(file.getName()));
byte[] bytes = new byte[1024]; int len = 0; while ((len = inputStream.read(bytes))!=-1){ zipOutputStream.write(bytes,0,len); } } catch (Exception e) { e.printStackTrace(); }finally { try { if (zipOutputStream!=null){ zipOutputStream.closeEntry(); zipOutputStream.close(); } if (outputStream!=null){ outputStream.close(); } if (inputStream!=null){ inputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
|
2.单文件解压
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
public class SingleZipUnpackThe {
private static InputStream inputStream; private static ZipInputStream bi; private static OutputStream fileOutputStream; private static String zipPath = "C:\\Users\\Surpass\\Desktop\\Bypass_1.14.2\\Bypass\\使用须知.zip";
public static void main(String[] args) { try { ZipFile zipFile = new ZipFile(zipPath, Charset.forName("GBK")); String zipFileParentPath = zipFile.getName().substring(0,zipFile.getName().lastIndexOf("\\")); System.out.println(zipFileParentPath);
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) { ZipEntry zipEntry = entries.nextElement(); fileOutputStream = new FileOutputStream(zipFileParentPath+"\\" + zipEntry.getName()); byte[] bytes = new byte[1024]; int len = 0; while ((len = bi.read(bytes))!=-1){ fileOutputStream.write(bytes,0,len); } } } catch (Exception e) { e.printStackTrace(); }finally { try { if (bi!=null){ bi.closeEntry(); bi.close(); } if (fileOutputStream!=null){ fileOutputStream.close(); } if (inputStream!=null){ inputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
|
3.多文件压缩(保留原有结构)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
|
public class MultipleFilesCompression {
private static InputStream inputStream; private static ZipOutputStream zipOutputStream; private static OutputStream outputStream;
private static String filePath = "C:\\Users\\Surpass\\Desktop\\Bypass_1.14.25";
public static void main(String[] args) { try { File file = new File(filePath); String dirName = file.getName(); String fileParentPath = file.getParent();
outputStream = new FileOutputStream(fileParentPath+"\\"+dirName+".zip"); zipOutputStream = new ZipOutputStream(outputStream);
Map<String,String> map = new HashMap<>(); map = getFile(file, map);
Set<String> keySet = map.keySet(); Iterator<String> iterator = keySet.iterator(); while (iterator.hasNext()){ String fileName = iterator.next(); String path = map.get(fileName); if (path.equals("")){ String[] basePath = fileName.split(dirName); String parent = basePath[1]; zipOutputStream.putNextEntry(new ZipEntry(parent+"/")); }else { inputStream = new FileInputStream(path+"\\"+fileName); String[] basePath = path.split(dirName); String parent = basePath[1]; zipOutputStream.putNextEntry(new ZipEntry(parent +"\\"+fileName)); } byte[] bytes = new byte[1024]; int len = 0; while ((len = inputStream.read(bytes))!=-1){ zipOutputStream.write(bytes,0,len); } } } catch (Exception e) { e.printStackTrace(); }finally { try { if (zipOutputStream!=null){ zipOutputStream.closeEntry(); zipOutputStream.close(); } if (outputStream!=null){ outputStream.close(); } if (inputStream!=null){ inputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } }
private static Map<String,String> getFile(File file,Map<String,String> map){ File[] files = file.listFiles(); if (files.length==0){ map.put(file.getAbsolutePath(),""); } for (File file1 : files) { if (file1.isDirectory()){ getFile(file1,map); } if (file1.isFile()){ map.put(file1.getName(),file1.getParent()); } } return map; } }
|
4.多文件解压(保留原有结构)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
public class MultipleFilesUnpackThe {
private static OutputStream outputStream; private static InputStream inputStream; private static ZipInputStream bi;
private static String ZipPath = "C:\\Users\\Surpass\\Desktop\\Bypass.zip";
public static void main(String[] args) {
try { ZipFile zipFile = new ZipFile(ZipPath, Charset.forName("GBK")); String zipName = zipFile.getName().substring(0, zipFile.getName().indexOf(".")); String zipFileParentPath = zipFile.getName().substring(0,zipFile.getName().lastIndexOf("\\"));
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); if (entry.isDirectory()) { File file = new File(zipFileParentPath+"\\"+zipName+"\\"+entry); file.mkdirs(); }else { String entryPath = entry.getName().substring(0,entry.getName().lastIndexOf("\\"));
File file = new File(zipFileParentPath+"\\"+zipName+"\\"+entryPath); if (!file.exists()) { file.mkdirs(); } outputStream = new FileOutputStream(zipFileParentPath+"\\"+zipName+"\\"+ entry.getName()); } byte[] bytes = new byte[1024]; int len = 0; while ((len = bi.read(bytes))!=-1){ outputStream.write(bytes,0,len); } } } catch (Exception e) { e.printStackTrace(); }finally { try { if (bi!=null){ bi.closeEntry(); bi.close(); } if (inputStream!=null){ inputStream.close(); } if (outputStream!=null){ outputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
|
总结
看网上的例子说有什么jar能简单一下,博主这里没有尝试去看,既然这样写了,也算是一个不错的练习。当然,博主道行较浅,代码不规范是一方面,如果有什么不足之处,还望各位大牛批评指正。