Java Code Implementation compressed file

Source: Internet
Author: User

Uploading files to the server
String TempPath = Req.getsession (). Getservletcontext (). Getrealpath ("webresources/templet/");

String filename = Importfile.getoriginalfilename (); @RequestParam ("ImportFile") Commonsmultipartfile ImportFile as a parameter pass the background

Extract zip file
String Zippath = temppath + file.separator + filename;
String zipfolder = filename.substring (0, Filename.length ()-4);
String Decompresspath = temppath + File.separator + zipfolder;

List<string> filepathlist =readbyapachezipfile (Zippath, Decompresspath);

@SuppressWarnings ("unchecked") Public list<string> readbyapachezipfile (string zippath, string decompresspath) t Hrows IOException,
FileNotFoundException, Zipexception {
File F = new file (Decompresspath);
if (!f.exists ()) {
F.mkdirs ();
}
list<string> filepathlist = new arraylist<string> ();
ZipFile ZF = new ZipFile (Zippath, "GBK");/support Chinese
Enumeration<zipentry> e = zf.getentries ();
while (E.hasmoreelements ()) {
ZipEntry ze2 = (zipentry) e.nextelement ();
String entryname = Ze2.getname ();
String Path = Decompresspath + "/" + entryname;
if (Ze2.isdirectory ()) {
SYSTEM.OUT.PRINTLN ("Creating Decompression Directory-" + EntryName);
File Decompressdirfile = new file (path);
if (!decompressdirfile.exists ()) {
Decompressdirfile.mkdirs ();
}
} else {
SYSTEM.OUT.PRINTLN ("Creating unzip file-" + EntryName);
String filedir = path.substring (0, Path.lastindexof ("/"));
File Filedirfile = new file (Filedir);
if (!filedirfile.exists ()) {
Filedirfile.mkdirs ();
}
Bufferedoutputstream BOS = new Bufferedoutputstream (new FileOutputStream (path));
Bufferedinputstream bi = new Bufferedinputstream (Zf.getinputstream (ze2));
byte[] readcontent = new byte[1024];
int readcount = Bi.read (readcontent);
while (Readcount!=-1) {
Bos.write (readcontent, 0, Readcount);
Readcount = Bi.read (readcontent);
}
Bos.close ();
String curdatestr = portalutils.getcurrentdatetimestring ();
String Renamepath = Decompresspath + "/" + entryname.substring (0, Entryname.lastindexof ("/") + 1)
+ Curdatestr + "_" + entryname.substring (Entryname.lastindexof ("/") + 1, entryname.length ());
File FF = new file (path);
Ff.renameto (New File (Renamepath));
Filepathlist.add (Renamepath);
}
}
Zf.close ();
return filepathlist;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.