Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JBTM-2111. Zip object store in case of XTS crash recovery test failure
- Loading branch information
gytis
committed
Mar 4, 2014
1 parent
a809192
commit c27554e
Showing
2 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
XTS/localjunit/crash-recovery-tests/src/test/java/com/arjuna/qa/junit/ZipArchiver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* Copyright 2014, Red Hat, Inc., and individual contributors | ||
* as indicated by the @author tags. See the copyright.txt file in the | ||
* distribution for a full listing of individual contributors. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
|
||
package com.arjuna.qa.junit; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.util.zip.ZipEntry; | ||
import java.util.zip.ZipOutputStream; | ||
|
||
/** | ||
* | ||
* @author <a href="mailto:gytis@redhat.com">Gytis Trikleris</a> | ||
* | ||
*/ | ||
public final class ZipArchiver { | ||
|
||
public void createArchive(final String source, final String target) throws IOException { | ||
final File targetFile = new File(target); | ||
final File sourceFile = new File(source); | ||
final FileOutputStream targetFileOutputStream = new FileOutputStream(targetFile); | ||
final ZipOutputStream targetZipOutputStream = new ZipOutputStream(targetFileOutputStream); | ||
|
||
archiveFile(sourceFile, targetZipOutputStream, new ZipEntry(sourceFile.getName())); | ||
|
||
targetZipOutputStream.close(); | ||
targetFileOutputStream.close(); | ||
} | ||
|
||
private void archiveDirectory(final File sourceDirectory, final ZipOutputStream targetOutputStream, | ||
final ZipEntry zipEntry) throws IOException { | ||
|
||
for (final File file : sourceDirectory.listFiles()) { | ||
final ZipEntry fileZipEntry = new ZipEntry(zipEntry.getName() + "/" + file.getName()); | ||
|
||
archiveFile(file, targetOutputStream, fileZipEntry); | ||
} | ||
} | ||
|
||
private void archiveFile(final File sourceFile, final ZipOutputStream targetOutputStream, | ||
final ZipEntry zipEntry) throws IOException { | ||
|
||
if (sourceFile.isDirectory()) { | ||
archiveDirectory(sourceFile, targetOutputStream, zipEntry); | ||
} else { | ||
byte[] buffer = new byte[1024]; | ||
final FileInputStream sourceInputStream = new FileInputStream(sourceFile); | ||
int lenght; | ||
|
||
targetOutputStream.putNextEntry(zipEntry); | ||
|
||
while ((lenght = sourceInputStream.read(buffer)) > 0) { | ||
targetOutputStream.write(buffer, 0, lenght); | ||
} | ||
|
||
sourceInputStream.close(); | ||
} | ||
} | ||
|
||
} |