| 1 | package org.j7zip.SevenZip;
|
|---|
| 2 |
|
|---|
| 3 | import java.io.File;
|
|---|
| 4 |
|
|---|
| 5 | import org.j7zip.SevenZip.Archive.IArchiveExtractCallback;
|
|---|
| 6 | import org.j7zip.SevenZip.Archive.IInArchive;
|
|---|
| 7 | import org.j7zip.SevenZip.Archive.SevenZipEntry;
|
|---|
| 8 |
|
|---|
| 9 | public class ArchiveExtractCallback implements IArchiveExtractCallback // , ICryptoGetTextPassword,
|
|---|
| 10 | {
|
|---|
| 11 |
|
|---|
| 12 | class OutputStream extends java.io.OutputStream {
|
|---|
| 13 | java.io.RandomAccessFile file;
|
|---|
| 14 |
|
|---|
| 15 | public OutputStream(java.io.RandomAccessFile f) {
|
|---|
| 16 | file = f;
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 | public void close() throws java.io.IOException {
|
|---|
| 20 | file.close();
|
|---|
| 21 | file = null;
|
|---|
| 22 | }
|
|---|
| 23 | /*
|
|---|
| 24 | public void flush() throws java.io.IOException {
|
|---|
| 25 | file.flush();
|
|---|
| 26 | }
|
|---|
| 27 | */
|
|---|
| 28 | public void write(byte[] b) throws java.io.IOException {
|
|---|
| 29 | file.write(b);
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | public void write(byte[] b, int off, int len) throws java.io.IOException {
|
|---|
| 33 | file.write(b,off,len);
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | public void write(int b) throws java.io.IOException {
|
|---|
| 37 | file.write(b);
|
|---|
| 38 | }
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | public int SetTotal(long size) {
|
|---|
| 42 | return HRESULT.S_OK;
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | public int SetCompleted(long completeValue) {
|
|---|
| 46 | return HRESULT.S_OK;
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | public void PrintString(String str) {
|
|---|
| 50 | System.out.print(str);
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | public void PrintNewLine() {
|
|---|
| 54 | System.out.println("");
|
|---|
| 55 | }
|
|---|
| 56 | public int PrepareOperation(int askExtractMode) {
|
|---|
| 57 | /*_extractMode = false;
|
|---|
| 58 | switch (askExtractMode) {
|
|---|
| 59 | case IInArchive.NExtract_NAskMode_kExtract:
|
|---|
| 60 | _extractMode = true;
|
|---|
| 61 | };*/
|
|---|
| 62 | switch (askExtractMode) {
|
|---|
| 63 | case IInArchive.NExtract_NAskMode_kExtract:
|
|---|
| 64 | PrintString("Extracting ");
|
|---|
| 65 | break;
|
|---|
| 66 | case IInArchive.NExtract_NAskMode_kTest:
|
|---|
| 67 | PrintString("Testing ");
|
|---|
| 68 | break;
|
|---|
| 69 | case IInArchive.NExtract_NAskMode_kSkip:
|
|---|
| 70 | PrintString("Skipping ");
|
|---|
| 71 | break;
|
|---|
| 72 | };
|
|---|
| 73 | PrintString(_filePath);
|
|---|
| 74 | return HRESULT.S_OK;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | public int SetOperationResult(int operationResult) throws java.io.IOException {
|
|---|
| 78 | switch(operationResult) {
|
|---|
| 79 | case IInArchive.NExtract_NOperationResult_kOK:
|
|---|
| 80 | break;
|
|---|
| 81 | default:
|
|---|
| 82 | {
|
|---|
| 83 | NumErrors++;
|
|---|
| 84 | PrintString(" ");
|
|---|
| 85 | switch(operationResult) {
|
|---|
| 86 | case IInArchive.NExtract_NOperationResult_kUnSupportedMethod:
|
|---|
| 87 | PrintString("Unsupported Method");
|
|---|
| 88 | break;
|
|---|
| 89 | case IInArchive.NExtract_NOperationResult_kCRCError:
|
|---|
| 90 | PrintString("CRC Failed");
|
|---|
| 91 | break;
|
|---|
| 92 | case IInArchive.NExtract_NOperationResult_kDataError:
|
|---|
| 93 | PrintString("Data Error");
|
|---|
| 94 | break;
|
|---|
| 95 | default:
|
|---|
| 96 | PrintString("Unknown Error");
|
|---|
| 97 | }
|
|---|
| 98 | PrintNewLine();
|
|---|
| 99 | }
|
|---|
| 100 | }
|
|---|
| 101 | /*
|
|---|
| 102 | if(_outFileStream != null && _processedFileInfo.UTCLastWriteTimeIsDefined)
|
|---|
| 103 | _outFileStreamSpec->File.SetLastWriteTime(&_processedFileInfo.UTCLastWriteTime);
|
|---|
| 104 | */
|
|---|
| 105 | if (_outFileStream != null) _outFileStream.close(); // _outFileStream.Release();
|
|---|
| 106 | /*
|
|---|
| 107 | if (_extractMode && _processedFileInfo.AttributesAreDefined)
|
|---|
| 108 | NFile::NDirectory::MySetFileAttributes(_diskFilePath, _processedFileInfo.Attributes);
|
|---|
| 109 | */
|
|---|
| 110 | return HRESULT.S_OK;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | java.io.OutputStream _outFileStream;
|
|---|
| 114 |
|
|---|
| 115 | public String outputDir = null;
|
|---|
| 116 |
|
|---|
| 117 | public int GetStream(int index,
|
|---|
| 118 | java.io.OutputStream [] outStream,
|
|---|
| 119 | int askExtractMode) throws java.io.IOException {
|
|---|
| 120 |
|
|---|
| 121 | outStream[0] = null;
|
|---|
| 122 |
|
|---|
| 123 | SevenZipEntry item = _archiveHandler.getEntry(index);
|
|---|
| 124 | _filePath = item.getName();
|
|---|
| 125 | _diskFilePath = outputDir == null ? _filePath : outputDir + File.separator + _filePath;
|
|---|
| 126 |
|
|---|
| 127 | file = new File(_diskFilePath);
|
|---|
| 128 |
|
|---|
| 129 | switch (askExtractMode) {
|
|---|
| 130 | case IInArchive.NExtract_NAskMode_kTest:
|
|---|
| 131 | return HRESULT.S_OK;
|
|---|
| 132 |
|
|---|
| 133 | case IInArchive.NExtract_NAskMode_kExtract:
|
|---|
| 134 |
|
|---|
| 135 | try {
|
|---|
| 136 | isDirectory = item.isDirectory();
|
|---|
| 137 |
|
|---|
| 138 | if (isDirectory) {
|
|---|
| 139 | if (file.isDirectory()) {
|
|---|
| 140 | return HRESULT.S_OK;
|
|---|
| 141 | }
|
|---|
| 142 | if (file.mkdirs())
|
|---|
| 143 | return HRESULT.S_OK;
|
|---|
| 144 | else
|
|---|
| 145 | return HRESULT.S_FALSE;
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 | File dirs = file.getParentFile();
|
|---|
| 150 | if (dirs != null) {
|
|---|
| 151 | if (!dirs.isDirectory())
|
|---|
| 152 | if (!dirs.mkdirs())
|
|---|
| 153 | return HRESULT.S_FALSE;
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | long pos = item.getPosition();
|
|---|
| 157 | if (pos == -1) {
|
|---|
| 158 | file.delete();
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | java.io.RandomAccessFile outStr = new java.io.RandomAccessFile(_diskFilePath,"rw");
|
|---|
| 162 |
|
|---|
| 163 | if (pos != -1) {
|
|---|
| 164 | outStr.seek(pos);
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | outStream[0] = new OutputStream(outStr);
|
|---|
| 168 | } catch (java.io.IOException e) {
|
|---|
| 169 | return HRESULT.S_FALSE;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | return HRESULT.S_OK;
|
|---|
| 173 |
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | // other case : skip ...
|
|---|
| 177 |
|
|---|
| 178 | return HRESULT.S_OK;
|
|---|
| 179 |
|
|---|
| 180 | }
|
|---|
| 181 |
|
|---|
| 182 | org.j7zip.SevenZip.Archive.IInArchive _archiveHandler; // IInArchive
|
|---|
| 183 | protected String _filePath; // name inside archive
|
|---|
| 184 | String _diskFilePath; // full path to file on disk
|
|---|
| 185 | protected File file;
|
|---|
| 186 |
|
|---|
| 187 | public long NumErrors;
|
|---|
| 188 | //boolean PasswordIsDefined;
|
|---|
| 189 | //String Password;
|
|---|
| 190 | //boolean _extractMode;
|
|---|
| 191 |
|
|---|
| 192 | boolean isDirectory;
|
|---|
| 193 |
|
|---|
| 194 | public ArchiveExtractCallback() { /*PasswordIsDefined = false;*/ }
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 | public void Init(org.j7zip.SevenZip.Archive.IInArchive archiveHandler) {
|
|---|
| 198 | NumErrors = 0;
|
|---|
| 199 | _archiveHandler = archiveHandler;
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | }
|
|---|