Visualizzazione dei risultati da 1 a 3 su 3

Discussione: [JAVA] "Zippare" file

  1. #1
    Utente di HTML.it
    Registrato dal
    Aug 2005
    Messaggi
    101

    zippare file in java

    ho bisogno di un aiuto la seguente classe esegue zip e unzip

    ma devo poter cercare un file in una directory del mio disco e lo stesso per unzippare.

    GRazie

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.util.Enumeration;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipFile;
    import java.util.zip.ZipInputStream;
    import java.util.zip.ZipOutputStream;
    import java.io.File;

    import java.io.FileOutputStream;


    /**
    * @author Admin
    *
    * To change the template for this generated type comment go to
    * Window>Preferences>Java>Code Generation>Code and Comments
    */
    public class compressore {


    public File comprimi (String[] _filenames) {

    // These are the files to include in the ZIP file


    // Create a buffer for reading the files
    byte[] buf = new byte[1024];
    File outfile = null;
    try {
    // Create the ZIP file
    String outFilename = "outfile.zip";
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFilename));
    outfile = new File(outFilename);


    // Compress the files
    for (int i=0; i<_filenames.length; i++) {
    FileInputStream in = new FileInputStream(_filenames[i]);

    // Add ZIP entry to output stream.
    out.putNextEntry(new ZipEntry(_filenames[i]));

    // Transfer bytes from the file to the ZIP file
    int len;
    while ((len = in.read(buf)) > 0) {
    out.write(buf, 0, len);
    }

    // Complete the entry
    out.closeEntry();
    in.close();
    }

    // Complete the ZIP file
    out.close();
    }catch (FileNotFoundException fnf) {

    }
    catch (IOException e) {
    }
    return outfile;
    }


    public void elenca (String _filename){
    try {
    // Open the ZIP file
    ZipFile zf = new ZipFile(_filename);

    // Enumerate each entry
    for (Enumeration entries = zf.entries(); entries.hasMoreElements() {
    // Get the entry name
    String zipEntryName = ((ZipEntry)entries.nextElement()).getName();
    }
    }
    catch (FileNotFoundException fnf) {

    }catch (IOException e) {
    }




    }
    public void decomprimi (String _filename)
    {

    try {
    // Open the ZIP file
    String inFilename = _filename;
    ZipInputStream in = new ZipInputStream(new FileInputStream(inFilename));

    // Get the first entry
    ZipEntry entry = in.getNextEntry();

    // Open the output file
    String outFilename = "o";
    OutputStream out = new FileOutputStream(outFilename);

    // Transfer bytes from the ZIP file to the output file
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
    out.write(buf, 0, len);
    }

    // Close the streams
    out.close();
    in.close();
    }
    catch (FileNotFoundException fnf) {

    }
    catch (IOException e) {
    }

    }
    }

  2. #2
    Utente di HTML.it L'avatar di floyd
    Registrato dal
    Apr 2001
    Messaggi
    3,837
    ma la domanda?

  3. #3
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    la classe così com'è funziona? Se devi solo personalizzare la selezione del file da zippare e l'output, la domanda (sottintesa e mai posta, come floyd fa notare) con zip/unizp non centra un ciclamino: cerca sul forum JFileChooser
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.