Author: damjan
Date: Mon Jun 10 15:22:14 2013
New Revision: 1491496

URL: http://svn.apache.org/r1491496
Log:
Add preliminary support for writing 7z archives.


Added:
    
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java
   (with props)
    
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/SevenZTestCase.java
   (with props)
Modified:
    
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java
    
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/package.html
    commons/proper/compress/trunk/src/site/xdoc/examples.xml
    commons/proper/compress/trunk/src/site/xdoc/index.xml

Modified: 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java
URL: 
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java?rev=1491496&r1=1491495&r2=1491496&view=diff
==============================================================================
--- 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java
 (original)
+++ 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZFile.java
 Mon Jun 10 15:22:14 2013
@@ -69,7 +69,7 @@ import org.apache.commons.compress.utils
  */
 public class SevenZFile {
     private static final boolean DEBUG = false;
-    private static final int SIGNATURE_HEADER_SIZE = 32;
+    static final int SIGNATURE_HEADER_SIZE = 32;
     private RandomAccessFile file;
     private final Archive archive;
     private int currentEntryIndex = -1;
@@ -78,7 +78,7 @@ public class SevenZFile {
     private InputStream currentEntryInputStream = null;
     private String password;
         
-    private static final byte[] sevenZSignature = {
+    static final byte[] sevenZSignature = {
         (byte)'7', (byte)'z', (byte)0xBC, (byte)0xAF, (byte)0x27, (byte)0x1C
     };
     

Added: 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java
URL: 
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java?rev=1491496&view=auto
==============================================================================
--- 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java
 (added)
+++ 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java
 Mon Jun 10 15:22:14 2013
@@ -0,0 +1,525 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+package org.apache.commons.compress.archivers.sevenz;
+
+import java.io.ByteArrayOutputStream;
+import java.io.DataOutput;
+import java.io.DataOutputStream;
+import java.io.File;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.util.ArrayList;
+import java.util.BitSet;
+import java.util.Date;
+import java.util.List;
+import java.util.zip.CRC32;
+
+import org.apache.commons.compress.archivers.ArchiveEntry;
+
+/**
+ * Writes a 7z file.
+ */
+public class SevenZOutputFile {
+    private final RandomAccessFile file;
+    private final List<SevenZArchiveEntry> files = new 
ArrayList<SevenZArchiveEntry>();
+    private int numNonEmptyStreams = 0;
+    private CRC32 crc32 = new CRC32();
+    private long fileBytesWritten = 0;
+    private boolean finished = false;
+    
+    public SevenZOutputFile(final File filename) throws IOException {
+        file = new RandomAccessFile(filename, "rw");
+        file.seek(SevenZFile.SIGNATURE_HEADER_SIZE);
+    }
+    
+    public void close() {
+        try {
+            if (!finished) {
+                finish();
+            }
+            file.close();
+        } catch (IOException ioEx) { // NOPMD
+        }
+    }
+    
+    public SevenZArchiveEntry createArchiveEntry(final File inputFile,
+            final String entryName) throws IOException {
+        final SevenZArchiveEntry entry = new SevenZArchiveEntry();
+        entry.setDirectory(inputFile.isDirectory());
+        entry.setName(entryName);
+        entry.setHasLastModifiedDate(true);
+        entry.setLastModifiedDate(new Date(inputFile.lastModified()));
+        return entry;
+    }
+
+    public void putArchiveEntry(final ArchiveEntry archiveEntry) throws 
IOException {
+        final SevenZArchiveEntry entry = (SevenZArchiveEntry) archiveEntry;
+        files.add(entry);
+    }
+    
+    public void closeArchiveEntry() throws IOException {
+        final SevenZArchiveEntry entry = files.get(files.size() - 1);
+        if (fileBytesWritten > 0) {
+            entry.setHasStream(true);
+            ++numNonEmptyStreams;
+            entry.setSize(fileBytesWritten);
+            entry.setCrc((int) crc32.getValue());
+            entry.setHasCrc(true);
+        } else {
+            entry.setHasStream(false);
+            entry.setSize(0);
+            entry.setHasCrc(false);
+        }
+        crc32.reset();
+        fileBytesWritten = 0;
+    }
+    
+    public void write(final int b) throws IOException {
+        file.write(b);
+        crc32.update(b);
+        fileBytesWritten++;
+    }
+    
+    public void write(final byte[] b) throws IOException {
+        write(b, 0, b.length);
+    }
+    
+    public void write(final byte[] b, final int off, final int len) throws 
IOException {
+        file.write(b, off, len);
+        crc32.update(b, off, len);
+        fileBytesWritten += len;
+    }
+    
+    public void finish() throws IOException {
+        if (finished) {
+            throw new IOException("This archive has already been finished");
+        }
+        finished = true;
+        
+        final long headerPosition = file.getFilePointer();
+        
+        final ByteArrayOutputStream headerBaos = new ByteArrayOutputStream();
+        final DataOutputStream header = new DataOutputStream(headerBaos);
+        
+        writeHeader(header);
+        header.flush();
+        final byte[] headerBytes = headerBaos.toByteArray();
+        file.write(headerBytes);
+        
+        final CRC32 crc32 = new CRC32();
+        
+        // signature header
+        file.seek(0);
+        file.write(SevenZFile.sevenZSignature);
+        // version
+        file.write(0);
+        file.write(2);
+        
+        // start header
+        final ByteArrayOutputStream startHeaderBaos = new 
ByteArrayOutputStream();
+        final DataOutputStream startHeaderStream = new 
DataOutputStream(startHeaderBaos);
+        startHeaderStream.writeLong(Long.reverseBytes(headerPosition - 
SevenZFile.SIGNATURE_HEADER_SIZE));
+        startHeaderStream.writeLong(Long.reverseBytes(0xffffFFFFL & 
headerBytes.length));
+        crc32.reset();
+        crc32.update(headerBytes);
+        
startHeaderStream.writeInt(Integer.reverseBytes((int)crc32.getValue()));
+        startHeaderStream.flush();
+        final byte[] startHeaderBytes = startHeaderBaos.toByteArray();
+        crc32.reset();
+        crc32.update(startHeaderBytes);
+        file.writeInt(Integer.reverseBytes((int)crc32.getValue()));
+        file.write(startHeaderBytes);
+    }
+    
+    private void writeHeader(final DataOutput header) throws IOException {
+        header.write(NID.kHeader);
+        
+        header.write(NID.kMainStreamsInfo);
+        writeStreamsInfo(header);
+        writeFilesInfo(header);
+        header.write(NID.kEnd);
+    }
+    
+    private void writeStreamsInfo(final DataOutput header) throws IOException {
+        if (numNonEmptyStreams > 0) {
+            writePackInfo(header);
+            writeUnpackInfo(header);
+        }
+        
+        writeSubStreamsInfo(header);
+        
+        header.write(NID.kEnd);
+    }
+    
+    private void writePackInfo(final DataOutput header) throws IOException {
+        // FIXME: this needs to use the compressed sizes/CRCs when we start 
supporting compression.
+        header.write(NID.kPackInfo);
+        
+        writeUint64(header, 0);
+        writeUint64(header, 0xffffFFFFL & numNonEmptyStreams);
+        
+        header.write(NID.kSize);
+        for (final SevenZArchiveEntry entry : files) {
+            if (entry.hasStream()) {
+                writeUint64(header, entry.getSize());
+            }
+        }
+        
+        header.write(NID.kCRC);
+        header.write(1);
+        for (final SevenZArchiveEntry entry : files) {
+            if (entry.hasStream()) {
+                header.writeInt(Integer.reverseBytes(entry.getCrc()));
+            }
+        }
+        
+        header.write(NID.kEnd);
+    }
+    
+    private void writeUnpackInfo(final DataOutput header) throws IOException {
+        header.write(NID.kUnpackInfo);
+        
+        header.write(NID.kFolder);
+        // FIXME: add real support for solid compression, and actual 
compression methods
+        writeUint64(header, numNonEmptyStreams);
+        header.write(0);
+        for (int i = 0; i < numNonEmptyStreams; i++) {
+            writeFolder(header);
+        }
+        
+        header.write(NID.kCodersUnpackSize);
+        for (final SevenZArchiveEntry entry : files) {
+            if (entry.hasStream()) {
+                writeUint64(header, entry.getSize());
+            }
+        }
+        
+        header.write(NID.kCRC);
+        header.write(1);
+        for (final SevenZArchiveEntry entry : files) {
+            if (entry.hasStream()) {
+                header.writeInt(Integer.reverseBytes(entry.getCrc()));
+            }
+        }
+        
+        header.write(NID.kEnd);
+    }
+    
+    private void writeFolder(final DataOutput header) throws IOException {
+        writeUint64(header, 1);
+        header.write(1);
+        header.write(0);
+    }
+    
+    private void writeSubStreamsInfo(final DataOutput header) throws 
IOException {
+        header.write(NID.kSubStreamsInfo);
+//        
+//        header.write(NID.kCRC);
+//        header.write(1);
+//        for (final SevenZArchiveEntry entry : files) {
+//            if (entry.getHasCrc()) {
+//                header.writeInt(Integer.reverseBytes(entry.getCrc()));
+//            }
+//        }
+//        
+        header.write(NID.kEnd);
+    }
+    
+    private void writeFilesInfo(final DataOutput header) throws IOException {
+        header.write(NID.kFilesInfo);
+        
+        writeUint64(header, files.size());
+
+        writeFileEmptyStreams(header);
+        writeFileEmptyFiles(header);
+        writeFileAntiItems(header);
+        writeFileNames(header);
+        writeFileCTimes(header);
+        writeFileATimes(header);
+        writeFileMTimes(header);
+        writeFileWindowsAttributes(header);
+        header.write(0);
+    }
+    
+    private void writeFileEmptyStreams(final DataOutput header) throws 
IOException {
+        boolean hasEmptyStreams = false;
+        for (final SevenZArchiveEntry entry : files) {
+            if (!entry.hasStream()) {
+                hasEmptyStreams = true;
+                break;
+            }
+        }
+        if (hasEmptyStreams) {
+            header.write(NID.kEmptyStream);
+            final BitSet emptyStreams = new BitSet(files.size());
+            for (int i = 0; i < files.size(); i++) {
+                emptyStreams.set(i, !files.get(i).hasStream());
+            }
+            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            final DataOutputStream out = new DataOutputStream(baos);
+            writeBits(out, emptyStreams, files.size());
+            out.flush();
+            final byte[] contents = baos.toByteArray();
+            writeUint64(header, contents.length);
+            header.write(contents);
+        }
+    }
+    
+    private void writeFileEmptyFiles(final DataOutput header) throws 
IOException {
+        boolean hasEmptyFiles = false;
+        for (final SevenZArchiveEntry entry : files) {
+            if (!entry.hasStream() && !entry.isDirectory()) {
+                hasEmptyFiles = true;
+                break;
+            }
+        }
+        if (hasEmptyFiles) {
+            header.write(NID.kEmptyFile);
+            final BitSet emptyFiles = new BitSet(files.size());
+            for (int i = 0; i < files.size(); i++) {
+                emptyFiles.set(i, !files.get(i).hasStream() && 
!files.get(i).isDirectory());
+            }
+            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            final DataOutputStream out = new DataOutputStream(baos);
+            writeBits(out, emptyFiles, files.size());
+            out.flush();
+            final byte[] contents = baos.toByteArray();
+            writeUint64(header, contents.length);
+            header.write(contents);
+        }
+    }
+    
+    private void writeFileAntiItems(final DataOutput header) throws 
IOException {
+        boolean hasAntiItems = false;
+        for (final SevenZArchiveEntry entry : files) {
+            if (entry.isAntiItem()) {
+                hasAntiItems = true;
+                break;
+            }
+        }
+        if (hasAntiItems) {
+            header.write(NID.kAnti);
+            final BitSet antiItems = new BitSet(files.size());
+            for (int i = 0; i < files.size(); i++) {
+                antiItems.set(i, files.get(i).isAntiItem());
+            }
+            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            final DataOutputStream out = new DataOutputStream(baos);
+            writeBits(out, antiItems, files.size());
+            out.flush();
+            final byte[] contents = baos.toByteArray();
+            writeUint64(header, contents.length);
+            header.write(contents);
+        }
+    }
+    
+    private void writeFileNames(final DataOutput header) throws IOException {
+        header.write(NID.kName);
+        
+        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+        final DataOutputStream out = new DataOutputStream(baos);
+        out.write(0);
+        for (final SevenZArchiveEntry entry : files) {
+            out.write(entry.getName().getBytes("UTF-16LE"));
+            out.writeShort(0);
+        }
+        out.flush();
+        final byte[] contents = baos.toByteArray();
+        writeUint64(header, contents.length);
+        header.write(contents);
+    }
+
+    private void writeFileCTimes(final DataOutput header) throws IOException {
+        int numCreationDates = 0;
+        for (final SevenZArchiveEntry entry : files) {
+            if (entry.getHasCreationDate()) {
+                ++numCreationDates;
+            }
+        }
+        if (numCreationDates > 0) {
+            header.write(NID.kCTime);
+
+            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            final DataOutputStream out = new DataOutputStream(baos);
+            if (numCreationDates != files.size()) {
+                out.write(0);
+                final BitSet cTimes = new BitSet(files.size());
+                for (int i = 0; i < files.size(); i++) {
+                    cTimes.set(i, files.get(i).getHasCreationDate());
+                }
+                writeBits(out, cTimes, files.size());
+            } else {
+                out.write(1);
+            }
+            out.write(0);
+            for (final SevenZArchiveEntry entry : files) {
+                if (entry.getHasCreationDate()) {
+                    out.writeLong(Long.reverseBytes(
+                            
SevenZArchiveEntry.javaTimeToNtfsTime(entry.getCreationDate())));
+                }
+            }
+            out.flush();
+            final byte[] contents = baos.toByteArray();
+            writeUint64(header, contents.length);
+            header.write(contents);
+        }
+    }
+
+    private void writeFileATimes(final DataOutput header) throws IOException {
+        int numAccessDates = 0;
+        for (final SevenZArchiveEntry entry : files) {
+            if (entry.getHasAccessDate()) {
+                ++numAccessDates;
+            }
+        }
+        if (numAccessDates > 0) {
+            header.write(NID.kATime);
+
+            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            final DataOutputStream out = new DataOutputStream(baos);
+            if (numAccessDates != files.size()) {
+                out.write(0);
+                final BitSet aTimes = new BitSet(files.size());
+                for (int i = 0; i < files.size(); i++) {
+                    aTimes.set(i, files.get(i).getHasAccessDate());
+                }
+                writeBits(out, aTimes, files.size());
+            } else {
+                out.write(1);
+            }
+            out.write(0);
+            for (final SevenZArchiveEntry entry : files) {
+                if (entry.getHasAccessDate()) {
+                    out.writeLong(Long.reverseBytes(
+                            
SevenZArchiveEntry.javaTimeToNtfsTime(entry.getAccessDate())));
+                }
+            }
+            out.flush();
+            final byte[] contents = baos.toByteArray();
+            writeUint64(header, contents.length);
+            header.write(contents);
+        }
+    }
+
+    private void writeFileMTimes(final DataOutput header) throws IOException {
+        int numLastModifiedDates = 0;
+        for (final SevenZArchiveEntry entry : files) {
+            if (entry.getHasLastModifiedDate()) {
+                ++numLastModifiedDates;
+            }
+        }
+        if (numLastModifiedDates > 0) {
+            header.write(NID.kMTime);
+
+            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            final DataOutputStream out = new DataOutputStream(baos);
+            if (numLastModifiedDates != files.size()) {
+                out.write(0);
+                final BitSet mTimes = new BitSet(files.size());
+                for (int i = 0; i < files.size(); i++) {
+                    mTimes.set(i, files.get(i).getHasLastModifiedDate());
+                }
+                writeBits(out, mTimes, files.size());
+            } else {
+                out.write(1);
+            }
+            out.write(0);
+            for (final SevenZArchiveEntry entry : files) {
+                if (entry.getHasLastModifiedDate()) {
+                    out.writeLong(Long.reverseBytes(
+                            
SevenZArchiveEntry.javaTimeToNtfsTime(entry.getLastModifiedDate())));
+                }
+            }
+            out.flush();
+            final byte[] contents = baos.toByteArray();
+            writeUint64(header, contents.length);
+            header.write(contents);
+        }
+    }
+
+    private void writeFileWindowsAttributes(final DataOutput header) throws 
IOException {
+        int numWindowsAttributes = 0;
+        for (final SevenZArchiveEntry entry : files) {
+            if (entry.getHasWindowsAttributes()) {
+                ++numWindowsAttributes;
+            }
+        }
+        if (numWindowsAttributes > 0) {
+            header.write(NID.kWinAttributes);
+
+            final ByteArrayOutputStream baos = new ByteArrayOutputStream();
+            final DataOutputStream out = new DataOutputStream(baos);
+            if (numWindowsAttributes != files.size()) {
+                out.write(0);
+                final BitSet attributes = new BitSet(files.size());
+                for (int i = 0; i < files.size(); i++) {
+                    attributes.set(i, files.get(i).getHasWindowsAttributes());
+                }
+                writeBits(out, attributes, files.size());
+            } else {
+                out.write(1);
+            }
+            out.write(0);
+            for (final SevenZArchiveEntry entry : files) {
+                if (entry.getHasWindowsAttributes()) {
+                    
out.writeInt(Integer.reverseBytes(entry.getWindowsAttributes()));
+                }
+            }
+            out.flush();
+            final byte[] contents = baos.toByteArray();
+            writeUint64(header, contents.length);
+            header.write(contents);
+        }
+    }
+
+    private void writeUint64(final DataOutput header, long value) throws 
IOException {
+        int firstByte = 0;
+        int mask = 0x80;
+        int i;
+        for (i = 0; i < 8; i++) {
+            if (value < ((1L << ( 7  * (i + 1))))) {
+                firstByte |= (value >>> (8 * i));
+                break;
+            }
+            firstByte |= mask;
+            mask >>>= 1;
+        }
+        header.write(firstByte);
+        for (; i > 0; i--) {
+            header.write((int) (0xff & value));
+            value >>>= 8;
+        }
+    }
+
+    private void writeBits(final DataOutput header, final BitSet bits, final 
int length) throws IOException {
+        int cache = 0;
+        int shift = 7;
+        for (int i = 0; i < length; i++) {
+            cache |= ((bits.get(i) ? 1 : 0) << shift);
+            --shift;
+            if (shift == 0) {
+                header.write(cache);
+                shift = 7;
+                cache = 0;
+            }
+        }
+        if (length > 0 && shift > 0) {
+            header.write(cache);
+        }
+    }
+}

Propchange: 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/SevenZOutputFile.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/package.html
URL: 
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/package.html?rev=1491496&r1=1491495&r2=1491496&view=diff
==============================================================================
--- 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/package.html
 (original)
+++ 
commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/archivers/sevenz/package.html
 Mon Jun 10 15:22:14 2013
@@ -18,7 +18,7 @@
 
 -->
   <body>
-    <p>Provides classes for reading archives using
+    <p>Provides classes for reading and writing archives using
       the 7z format.</p>
   </body>
 </html>

Modified: commons/proper/compress/trunk/src/site/xdoc/examples.xml
URL: 
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/site/xdoc/examples.xml?rev=1491496&r1=1491495&r2=1491496&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/site/xdoc/examples.xml (original)
+++ commons/proper/compress/trunk/src/site/xdoc/examples.xml Mon Jun 10 
15:22:14 2013
@@ -432,6 +432,15 @@ xzIn.close();
         LZMA2 and no header compression at all.  Only AES-256/SHA-256
         are supported for encryption.</p>
 
+        <p>Adding an entry to a 7z archive:</p>
+<source><![CDATA[
+SevenZOutputFile sevenZOutput = new SevenZOutputFile(file);
+SevenZArchiveEntry entry = sevenZOutput.createArchiveEntry(fileToArchive, 
name);
+sevenZOutput.putArchiveEntry(entry);
+sevenZOutput.write(contentOfEntry);
+sevenZOutput.closeArchiveEntry();
+]]></source>
+
         <p>Uncompressing a given 7z archive (you would
           certainly add exception handling and make sure all streams
           get closed properly):</p>

Modified: commons/proper/compress/trunk/src/site/xdoc/index.xml
URL: 
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/site/xdoc/index.xml?rev=1491496&r1=1491495&r2=1491496&view=diff
==============================================================================
--- commons/proper/compress/trunk/src/site/xdoc/index.xml (original)
+++ commons/proper/compress/trunk/src/site/xdoc/index.xml Mon Jun 10 15:22:14 
2013
@@ -76,7 +76,7 @@
             archivers where the <a href="zip.html">zip</a>
             implementation provides capabilities that go beyond the
             features found in java.util.zip.  As of Commons Compress
-            1.6 support for the dump, 7z and arj formats is
+            1.6 support for the dump and arj formats is
             read-only.</p>
 
           <p>The compress component provides abstract base classes for

Added: 
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/SevenZTestCase.java
URL: 
http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/SevenZTestCase.java?rev=1491496&view=auto
==============================================================================
--- 
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/SevenZTestCase.java
 (added)
+++ 
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/SevenZTestCase.java
 Mon Jun 10 15:22:14 2013
@@ -0,0 +1,85 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+package org.apache.commons.compress.archivers;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+
+import org.apache.commons.compress.AbstractTestCase;
+import org.apache.commons.compress.archivers.sevenz.SevenZArchiveEntry;
+import org.apache.commons.compress.archivers.sevenz.SevenZFile;
+import org.apache.commons.compress.archivers.sevenz.SevenZOutputFile;
+
+public class SevenZTestCase extends AbstractTestCase {
+    public void testSevenZArchiveCreation() throws Exception {
+        final File output = new File(dir, "bla.7z");
+        final File file1 = getFile("test1.xml");
+        final File file2 = getFile("test2.xml");
+
+        final SevenZOutputFile outArchive = new SevenZOutputFile(output);
+        try {
+            SevenZArchiveEntry entry;
+            
+            entry = outArchive.createArchiveEntry(file1, file1.getName());
+            outArchive.putArchiveEntry(entry);
+            copy(file1, outArchive);
+            outArchive.closeArchiveEntry();
+            
+            entry = outArchive.createArchiveEntry(file2, file2.getName());
+            outArchive.putArchiveEntry(entry);
+            copy(file2, outArchive);
+            outArchive.closeArchiveEntry();
+        } finally {
+            outArchive.close();
+        }
+        
+        final SevenZFile archive = new SevenZFile(output);
+        try {
+            SevenZArchiveEntry entry;
+            
+            entry = archive.getNextEntry();
+            assert(entry != null);
+            assertEquals(entry.getName(), file1.getName());
+            
+            entry = archive.getNextEntry();
+            assert(entry != null);
+            assertEquals(entry.getName(), file2.getName());
+            
+            assert(archive.getNextEntry() == null);
+        } finally {
+            archive.close();
+        }
+    }
+    
+    private void copy(final File src, final SevenZOutputFile dst) throws 
IOException { 
+        FileInputStream fis = null;
+        try {
+            fis = new FileInputStream(src);
+            final byte[] buffer = new byte[8*1024];
+            int bytesRead;
+            while ((bytesRead = fis.read(buffer)) >= 0) {
+                dst.write(buffer, 0, bytesRead);
+            }
+        } finally {
+            if (fis != null) {
+                fis.close();
+            }
+        }
+    }
+}

Propchange: 
commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/archivers/SevenZTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to