JAVA implementation of multiple files in a ZIP compression package export __java

Source: Internet
Author: User

1, use Java to implement the picture of the server packaged into a ZIP format compression package export, multiple files packaged export.
2, the code is as follows:

**imagebyteutil.java** public class imagebyteutil{private static float QUALITY = 0.6f; public static void Compresszip (List<file> listfiles, OutputStream output,string encode, Boolean compress,string
    Alias) {Zipoutputstream zipstream = null;
            try {zipstream = new Zipoutputstream (output); 
            for (file file:listfiles) {compresszip (file, ZIPstream, compress,alias+ "_" + (Listfiles.indexof (file) +1));
        } catch (Exception e) {e.printstacktrace ();  
                }finally {try {if (ZIPstream!= null) {zipstream.close ();  
            } catch (IOException e) {e.printstacktrace (); }} private static void Compresszip (file file, Zipoutputstream ZIPstream, Boolean compress,st
        Ring alias) throws exception{FileInputStream input = null; try {input = newFileInputStream (file); 
            Zip (input, ZIPstream, File.getname (), compress); Zip (input, ZIPstream, alias+ ".") +file.getname (). substring (File.getname (). LastIndexOf (".")
        +1), compress);
        catch (Exception e) {e.printstacktrace ();  
            }finally {try {if (input!= null) input.close ();  
            catch (IOException e) {e.printstacktrace (); }} private static void Zip (InputStream input, Zipoutputstream zipstream, String zipentryname  
        , Boolean compress) throws exception{byte[] bytes = NULL;  
        Bufferedinputstream bufferstream = null; try {if (input = = null) throw new Exception ("Get Compressed data item failed!")  
            The data item name is: "+ zipentryname";
            A compressed entry is not a specific stand-alone file, but a list item in a compressed package file list, called an entry, just like an index zipentry zipentry = new ZipEntry ("Picture/" +zipentryname); Navigate to the location of the compressed entry and begin writing to the fileZipstream.putnextentry (ZipEntry) in the compressed package;  
                if (compress) {bytes = imagebyteutil.compressofquality (input, 0);  
            Zipstream.write (bytes, 0, bytes.length); else {bytes = new byte[1024 * 5];//read-write buffer bufferstream = new Bufferedinputstream (Inpu  
                t);//input buffer stream int read = 0;  
                while (read = Bufferstream.read (bytes))!=-1) {zipstream.write (bytes, 0, read);  
        A catch (IOException e) {e.printstacktrace ());  
            Finally {try {if (null!= bufferstream) bufferstream.close ();  
            catch (IOException e) {e.printstacktrace ();  }} public static byte[] compressofquality (file file, float quality) throws exception{byte[]  
        BS = null; InputStream Input = null;  
            try {input = new FileInputStream (file);
        BS = compressofquality (input,quality);  
        catch (Exception e) {e.printstacktrace ();  
            Finally {try {if (input!= null) input.close ();  
            catch (IOException e) {e.printstacktrace ();
    } return BS;
            public static byte[] compressofquality (inputstream input, float quality) throws Exception {  
        Bytearrayoutputstream output = null;  
            try {output = new Bytearrayoutputstream (); if (quality = = 0) {thumbnails.of (input). scale (1f). Outputquality (Quality). Tooutputstrea  
            m (output);  
            else {thumbnails.of (input). scale (1f). Outputquality (Quality). Tooutputstream (output);  return Output.tobytearray ();
        catch (Exception e) {e.printstacktrace ();  
            Finally {try {if (output!= null) output.close ();  
            catch (IOException e) {e.printstacktrace ();
    } return null; }
}
**main.java** public

static void Main (string[] args) {
    //collection of files to export, add the files that you want to export
    list<file> listfiles = new arraylist<> ();
    Invoke the tool class, the parameter description (the set of files that needs to be exported, the Bytearrayoutputstream object, the encoding, whether to compress "True,false", the file name prefix)
    imagebyteutil.compresszip ( Listfiles, out, "UTF-8", False, "LWJ");
    Specifies the export format
    returncontext.addparam ("Exportfilename", "Extfile.zip");
    Returncontext.addparam ("MimeType", "zip");
    return in;
}

3, this function is based on the development process of the project needs to be implemented, testing can be used normally, can change the customization.

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.