Bonjour,
Je cherche à utiliser les API sevenzipjbinding-4.65.1.jar et sevenzipjbinding-AllPlatforms.jar pour extraire des archives .7z.
Après diverses recherches sur internet, je suis arrivé au code suvant :
Mon problème est que si mon archive contient de gros fichiers textes, le fichier qui est extrait n'est pas complet. Je n'optient que la fin du fichier.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.RandomAccessFile; import java.util.ArrayList; import java.util.List; import net.sf.sevenzipjbinding.ExtractAskMode; import net.sf.sevenzipjbinding.ExtractOperationResult; import net.sf.sevenzipjbinding.IArchiveExtractCallback; import net.sf.sevenzipjbinding.ISequentialOutStream; import net.sf.sevenzipjbinding.ISevenZipInArchive; import net.sf.sevenzipjbinding.PropID; import net.sf.sevenzipjbinding.SevenZip; import net.sf.sevenzipjbinding.SevenZipException; import net.sf.sevenzipjbinding.impl.RandomAccessFileInStream; public class TestZip { private static String repertoireExtract; public static class MyExtractCallback implements IArchiveExtractCallback { private ISevenZipInArchive inArchive; public MyExtractCallback(ISevenZipInArchive inArchive) {this.inArchive = inArchive;} public ISequentialOutStream getStream(final int index, ExtractAskMode extractAskMode) throws SevenZipException { if (extractAskMode != ExtractAskMode.EXTRACT) { return null; } return new ISequentialOutStream() { public int write(byte[] data) throws SevenZipException { FileOutputStream fos; String fichierDestination=""; try { File file = new File((String) inArchive.getProperty(index, PropID.PATH)); fichierDestination=repertoireExtract+ file.getName(); file = new File(fichierDestination); fos = new FileOutputStream(file); fos.write(data); fos.close(); } catch (FileNotFoundException e) {e.printStackTrace(); } catch (IOException e) {e.printStackTrace();} System.out.println(fichierDestination+" : "+data.length); return data.length; } }; } public void prepareOperation(ExtractAskMode extractAskMode) throws SevenZipException {} public void setOperationResult(ExtractOperationResult extractOperationResult) throws SevenZipException {} public void setCompleted(long completeValue) throws SevenZipException {} public void setTotal(long total) throws SevenZipException {} } public static void main(String[] args) { RandomAccessFile randomAccessFile = null; ISevenZipInArchive inArchive = null; repertoireExtract="f:\\Test\\"; try { randomAccessFile = new RandomAccessFile("f:\\Archive.7z", "r"); inArchive = SevenZip.openInArchive(null, new RandomAccessFileInStream(randomAccessFile)); int count = inArchive.getNumberOfItems(); List<Integer> itemsToExtract = new ArrayList<Integer>(); for (int i = 0; i < count; i++) { if (!((Boolean) inArchive.getProperty(i, PropID.IS_FOLDER)).booleanValue()) { itemsToExtract.add(Integer.valueOf(i)); } } int[] items = new int[itemsToExtract.size()]; int i = 0; for (Integer integer : itemsToExtract) { items[i++] = integer.intValue(); } inArchive.extract(items, false, new MyExtractCallback(inArchive)); } catch (Exception e) { System.err.println("Error occurs: " + e); System.exit(1); } finally { if (inArchive != null) { try {inArchive.close();} catch (SevenZipException e) {} } if (randomAccessFile != null) { try {randomAccessFile.close();} catch (IOException e) {} } } } }
Si quelqu'un peut m'aporter une solution, par avance merci.
Partager