tutorials.de Buch-Aktion 05/2012
ERLEDIGT
NEIN
ANTWORTEN
9
ZUGRIFFE
1012
EMPFEHLEN
  • An Twitter übertragen
  • An Facebook übertragen
AUF DIESES THEMA
ANTWORTEN
  1. #1
    wraith07 wraith07 ist offline Mitglied Bronze
    Registriert seit
    Oct 2009
    Beiträge
    29
    Hallo Leute,
    ich habe da folgendes Problem. Und zwar muss ich ein Zip Archiv von einer URL
    runterladen.
    Soweit OK.

    Code :
    1
    2
    3
    4
    
    URL url = new URL(inurl);
    InputStream instr = url.openStream();
    System.out.println(url.getProtocol()+": "+url.getHost()+url.getPath());
    ZipInputStream inzip = new ZipInputStream(instr);

    Um mir nun die Arbeit zu erleichetern habe ich hier im Forum
    das gefunden:
    http://www.tutorials.de/forum/java/2...entpacken.html

    Mit Hilfe diese Beispiels bin ich sweit gekommen,
    dass ich die Verzeichnisstruktur aus dem Archiv bekomme und diese auf die
    Platte schreibe.

    Problem gibt es aber bei den einzelnen Dateien im Archiv,
    die bekomme ich nicht geschrieben.

    Hilfe........
     

  2. #2
    Avatar von zerix
    zerix zerix ist offline Hausmeister
    tutorials.de Moderator
    Registriert seit
    May 2005
    Beiträge
    4.335
    Hallo,

    was genau funktioniert denn nicht?
    Kommt eine Fehlermeldung?

    Zeigst du mal den Code?

    Gruß

    Sascha
     
    Es ist schwer Allwissend zu sein. Aber ich komme damit klar. ;-)

  3. #3
    wraith07 wraith07 ist offline Mitglied Bronze
    Registriert seit
    Oct 2009
    Beiträge
    29
    Zitat Zitat von Sascha Schirra Beitrag anzeigen
    Hallo,

    was genau funktioniert denn nicht?
    Kommt eine Fehlermeldung?

    Zeigst du mal den Code?

    Gruß

    Sascha
    hallo,
    meine problem besteht darin, dass ich aktuell wie im beispiel
    http://www.tutorials.de/forum/java/2...entpacken.html
    vorgehe.

    ich hole mir über die url ein zip file und entpacke dieses sogleich in ein verzeichnis,
    hierbei umss ich leider das zip file in einer tmp datei ablegen.
    das möchte ich umgehen und nur über stream arbeiten.
     

  4. #4
    Registriert seit
    Jun 2002
    Ort
    Saarbrücken (Saarland)
    Beiträge
    9.886
    Blog-Einträge
    29
    Hallo,

    schau mal hier:
    Code java:
    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
    
    package de.tutorials;
     
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.URL;
    import java.nio.channels.Channels;
    import java.util.Iterator;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;
     
    import com.google.common.collect.AbstractIterator;
     
    public class ZipArchiveExtractor {
     
        /**
         * @param args
         */
        public static void main(String[] args) throws Exception {
     
            File destDir = new File("/tmp/goog.collect.src");
     
            final ZipInputStream zipIn = new ZipInputStream(new URL("http://localhost:8000/src-1.0-rc3.zip").openStream());
     
            Iterator<? extends ZipEntry> zipEntries = new AbstractIterator<ZipEntry>() {
                @Override
                protected ZipEntry computeNext() {
                    ZipEntry next = null;
                    try {
                        next = zipIn.getNextEntry();
                        if (next == null) {
                            next = endOfData();
                        }
                    } catch (IOException e) {
                        next = endOfData();
                    }
                    return next;
                }
            };
     
            while (zipEntries.hasNext()) {
                ZipEntry zipEntry = zipEntries.next();
                String entryName = zipEntry.getName();
     
                System.out.println(entryName);
                if (zipEntry.isDirectory()) {
                    File dir = new File(destDir, entryName);
                    if (!dir.exists()) {
                        if (!dir.mkdirs()) {
                            throw new RuntimeException("Cannot create directory: " + dir);
                        }
                    }
                } else {
     
                    FileOutputStream fos = null;
                    try {
                        File file = new File(destDir, entryName);
                        System.out.println(file);
                        fos = new FileOutputStream(file);
                        fos.getChannel().transferFrom(Channels.newChannel(zipIn), 0, zipEntry.getSize());
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        if (fos != null) {
                            fos.close();
                        }
                    }
                }
     
                zipIn.closeEntry();
            }
            zipIn.close();
     
        }
    }

    Gruß Tom
     
    Java rocks!
    How to become a good Java Programmer?
    Does IT in Java and .Net
    The only valid measurement of code quality: WTFs / minute
    Blog
    Xing
    Twitter

  5. #5
    wraith07 wraith07 ist offline Mitglied Bronze
    Registriert seit
    Oct 2009
    Beiträge
    29
    ja vielen dank,
    finde aber

    com.google.common.collect.AbstractIterator

    nicht zum runterlade
     

  6. #6
    wraith07 wraith07 ist offline Mitglied Bronze
    Registriert seit
    Oct 2009
    Beiträge
    29
    hi tom,
    ja dank klapp bis auf die kleinigkeit,
    dass die entpackten dateien leer sind.
     

  7. #7
    Registriert seit
    Jun 2002
    Ort
    Saarbrücken (Saarland)
    Beiträge
    9.886
    Blog-Einträge
    29
    Hallo,

    seltsam bei mir funktioniert das so einwandfrei (Ubuntu Linux 9.10 / OpenJDK 1.6, 1.6.0_0-b16) ... haben die Daten denn überhaupt Inhalt, wenn du das Zip Archiv von der Quelle manuell runterlädst?

    Gruß Tom
     
    Java rocks!
    How to become a good Java Programmer?
    Does IT in Java and .Net
    The only valid measurement of code quality: WTFs / minute
    Blog
    Xing
    Twitter

  8. #8
    wraith07 wraith07 ist offline Mitglied Bronze
    Registriert seit
    Oct 2009
    Beiträge
    29
    hallo tom,
    bei mir sieht die quelle "url" wie folgt aus

    "http://pgrc-35.ipk-gatersleben.de/bfiler-web/BFileDownloadServlet?table_name=gcc_images&pk_column=tab_id&pk_value=6,12,34,1,2,3,55,66,1,2,3,4,5,7 ,8,9,10&bfile_column=thumbs&calling_user=GCC"

    die entsprechende zip datei wird mittels eines servlets erzeug.........

    das beispiel
    http://www.tutorials.de/forum/java/2...entpacken.html

    funktioniert.
     

  9. #9
    wraith07 wraith07 ist offline Mitglied Bronze
    Registriert seit
    Oct 2009
    Beiträge
    29
    hi tom,
    ich habe es mit einigen änderungen hin bekommen.
    Code :
    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
    
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.Iterator;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;
    import com.google.common.collect.AbstractIterator;
     
    public class LimsZipExtractor{
        
        public LimsZipExtractor(String inurl) throws MalformedURLException, IOException{
           extract(inurl);
        };
        
        private void extract(String inurl) throws MalformedURLException, IOException{
            File destDir = new File("u:/test/"); // noch weg 
            byte[] buffer = new byte[16384];
            final ZipInputStream zipIn = new ZipInputStream(new URL(inurl).openStream());
     
            Iterator<? extends ZipEntry> zipEntries = new AbstractIterator<ZipEntry>() {
                protected ZipEntry computeNext() {
                    ZipEntry next = null;
                    try {
                        next = zipIn.getNextEntry();
                        if (next == null) {
                            next = endOfData();
                        }
                    } catch (IOException e) {
                        next = endOfData();
                    }
                    return next;
                }
            };
     
            while (zipEntries.hasNext()) {
                ZipEntry zipEntry = zipEntries.next();
                String entryName = zipEntry.getName();
     
                System.out.println(entryName);
                if (zipEntry.isDirectory()) {
                    File dir = new File(destDir, entryName);
                    if (!dir.exists()) {
                        if (!dir.mkdirs()) {
                            throw new RuntimeException("Cannot create directory: " + dir);
                        }
                    }
                } else {
     
                    BufferedOutputStream bos = new BufferedOutputStream(
                            new FileOutputStream(new File(destDir, entryName)));
     
                    
                    int len =0;
                    BufferedInputStream bis = new BufferedInputStream(zipIn);
                    while ((len = bis.read(buffer)) != -1) {
                        bos.write(buffer, 0, len);
                    }
                    bos.close();
                    zipIn.closeEntry();
                }
            } ; 
            
        };
     

  10. #10
    wraith07 wraith07 ist offline Mitglied Bronze
    Registriert seit
    Oct 2009
    Beiträge
    29
    hallo,
    leider noch eine frage,
    wie bekomme ich heraus wiele entries im stream sind

    Code :
    1
    2
    3
    4
    5
    6
    
     int count=0;
            while(zipIn.getNextEntry() !=null){
                count++;
                
            }
            System.out.println(count);

    macht nur eine endlosschleife.......
     

Ähnliche Themen

  1. Zip entpacken
    Von Peter86 im Forum .NET Café
    Antworten: 15
    Letzter Beitrag: 09.10.11, 14:27
  2. zip entpacken
    Von MiRaMC im Forum Java
    Antworten: 11
    Letzter Beitrag: 08.08.11, 19:54
  3. zip entpacken
    Von Sturm im Forum PHP
    Antworten: 3
    Letzter Beitrag: 09.01.07, 19:53
  4. *.gz entpacken
    Von ChuloGT im Forum PHP
    Antworten: 2
    Letzter Beitrag: 02.11.04, 13:53
  5. Entpacken von .zip
    Von rauchmelder im Forum PHP
    Antworten: 1
    Letzter Beitrag: 05.09.04, 19:31